Pages: [1]   Go Down
Author Topic: [SOLVED] Number/Hash sign in URL after submitting form  (Read 577 times)
0 Members and 1 Guest are viewing this topic.
Everett, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Arduino gurus.

I have developed a small program that will read the request header and returns a response header and message to client.

Everything works great except one small annoying thing.

When I submit the form, it appends a number/hash symbol to the URL. I have set the form action to post to itself (method is POST).

Please check it out here: http://arduino.martin.us.com

Any help would be highly appreciated.
« Last Edit: February 05, 2013, 03:32:39 pm by mtruijen » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5367
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Seemed ok from here. Maybe you should post your code.
Logged

Everett, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In order to reproduce, you would have to submit the page a couple times...
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


First, I suggest you fix the HTML problems...
http://validator.w3.org/check?uri=http%3A%2F%2Farduino.martin.us.com%2F&charset=%28detect+automatically%29&doctype=Inline&group=0

Maybe that will resolve the hash problem.
Logged

Everett, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great suggestion to run it through a validator. Some embarrassing mistakes. Got them fixed. Still the same issue ...
Logged

Everett, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's my code:


Code:
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,20);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

const char* iContentLengthPtr;
const char* kContentLengthPrefix = "Content-Length: ";

typedef enum {
  eIdle,
  eStatusCodeRead,
  eReadingContentLength,
  eReadBody,
  eSkipToStartOfBody,
  eReadingBody,
  eReadFormElement
} tHttpState;

int laserPin = 9;

void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  // initialize the digital pin as an output.
  pinMode(laserPin, OUTPUT);    

}

void loop() {

  tHttpState iState = eStatusCodeRead;
  int iContentLength = 0;
  int iElementCount;
  
  iContentLengthPtr = kContentLengthPrefix;

  char e[2][16];
  char *element;

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {

    // client.available() returns the number of bytes the client has yet to return
    // This number decreases everytime we do a client.read()
    while (client.available() > 0) {

        char c = client.read();
        Serial.write(c);

        switch(iState) {

          case eStatusCodeRead:
            if (*iContentLengthPtr == c) {
              iContentLengthPtr++;
              if (*iContentLengthPtr == '\0') {
                iState = eReadingContentLength;
              }
            } else {
              iContentLengthPtr = kContentLengthPrefix;
            }
            break;

          case eReadingContentLength:
            if (isdigit(c)) {
              iContentLength = iContentLength*10 + (c - '0');
            } else {
              iState = eSkipToStartOfBody;
            }
            break;

          case eSkipToStartOfBody:
            if (client.available() <= iContentLength) {
              iState = eReadingBody;
              iElementCount = 0;
            }
            break;
          
          case eReadingBody:
              if (c == '=') {
                iElementCount++;
                memset(e[iElementCount], 0, 16);
                element = e[iElementCount];
                iState = eReadFormElement;
              }
            break;

          case eReadFormElement:
            if (c != '&') {
              *element++ = c;
            } else {
              iState = eReadingBody;
            }
            break;

          default:
            break;
        }


    }

    if (strcmp(e[1],"on") == 0) {
      digitalWrite(laserPin, HIGH);
    } else {
      digitalWrite(laserPin, LOW);
    }

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html charset=utf-8");
    client.println("Connnection: close");
    client.println();
    client.println("<!DOCTYPE html>");
    client.println("<html>");
    client.println("<head>");
    client.println("<meta charset=\"UTF-8\">");
    client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
    client.println("<link rel=\"stylesheet\" href=\"http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css\" />");
    client.println("<script src=\"http://code.jquery.com/jquery-1.8.2.min.js\"></script>");
    client.println("<script src=\"http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js\"></script>");
    client.println("<title>Martien's Arduino</title>");
    client.println("</head>");
    client.println("<body>");
    client.println("<div data-role=\"page\">");
    client.println("<div data-role=\"header\">");
    client.println("<h1>Martien</h1>");
    client.println("</div>");
    client.println("<div data-role=\"content\">");
    client.println("<form method=post action=\"http://arduino.martin.us.com/\">");
    client.println("<fieldset data-role=\"fieldcontain\">");
    client.println("<label for=\"flip-1\">Laser</label>");
    client.println("<select name=\"flip-1\" id=\"flip-1\" data-role=\"slider\" data-mini=\"true\">");
    client.write("<option value=\"off\"");
      if(!bitRead(PORTB, 1)) client.write(" selected");
      client.println(">Off</option>");
    client.write("<option value=\"on\"");
      if(bitRead(PORTB, 1)) client.write(" selected");
      client.println(">On</option>");
    client.println("</select>");
    client.println("</fieldset>");
    client.println("<input type=submit value=Submit />");
    client.println("</form>");
    client.println("</div>");
    client.println("</div>");
    client.println("</body>");
    client.println("</html>");
    client.stop();

  }
}


Moderator edit: [code] [/code] tags added.
« Last Edit: February 05, 2013, 03:36:44 am by Coding Badly » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When I submit the form, it appends a number/hash symbol to the URL.

jQuery appears to be the culprit.
Logged

Everett, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh really? Thanks so much for finding that out. I will see if I can figure out what in JQuery is causing this.
Logged

Everett, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Added data-ajax="false" to the form element and issue was resolved  smiley
Logged

Pages: [1]   Go Up
Jump to: