Go Down

Topic: [SOLVED] Number/Hash sign in URL after submitting form (Read 963 times) previous topic - next topic

mtruijen

Feb 04, 2013, 10:33 pm Last Edit: Feb 05, 2013, 09:32 pm by mtruijen Reason: 1
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.

SurferTim

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

mtruijen

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

Coding Badly


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.

mtruijen

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

mtruijen

#5
Feb 05, 2013, 08:54 am Last Edit: Feb 05, 2013, 09:36 am by Coding Badly Reason: 1
Here's my code:


Code: [Select]
#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.

Coding Badly

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


jQuery appears to be the culprit.

mtruijen

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

mtruijen

Added data-ajax="false" to the form element and issue was resolved  :)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy