1

I am using an Arduino Mega and I have a basic webpage built using EthernetServer for the connection and EthernetClient to write the HTML to the client. I have a simple web form:

client.println("<form method=get>");
client.println("<input type=text name=data>");
client.println("<input type=submit value=Submit>");
client.println("</form>");

I'd like to receive any input from the form and check it against values in order to do stuff on the back end (e.g. reset certain data). How do I get the data from the web page?

2

Your form method is set to GET. In this case, when Submit is clicked, the client/browser sends request with values in URI:

GET /index.html?data=user_value&default_name_of_button=Submit HTTP/1.1
...more lines to follow...

These HTTP header lines will be available on your EthernetClient instance, retrieve them line-by-line using connected(), available(), read() methods. Then parse the line, find the user_value.

Please note that browser retrieves your page (to display it) using GET method too, in that case URI will be like:

GET /index.html HTTP/1.1
...more lines to follow...

It is recommended to use POST method to submit form values. Example content section (before content, server must send HTTP header - check Arduino Web Server example)

<form method="POST" action="foo.php">

First Name: <input type="text" name="first_name" /> <br />
Last Name: <input type="text" name="last_name" /> <br />

<input type="submit" name="action" value="Submit" />

</form>

In this case the values are returned in content section:

POST /foo.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost/test.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 43

first_name=John&last_name=Doe&action=Submit

Tutorial on HTTP headers:

https://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.