ESP with AJAX - Text Fields
Posted: Thu Nov 07, 2019 3:27 pm
Hello everyone,
I have a full functional website for a DHT22 sensor (thermostat) which I made only using simple HTML code and I am trying to improve the webserver communication by replacing the old school HTML with AJAX so whenever I send a command I don't have to refresh the page or change the page. I have to mention that I don't know JavaScript but I'm trying to understand the code.
the simple code I'm using is:
the button and textfield form I'm using is:
and in void setup I'm using this handling, so when the button is clicked it sends me to the /save page and the value of the variable changes.
Can someone explain how to implement this with AJAX (GET, SET) so when I complete the text field form with my temp. value and I press on the button there should be no refresh to change the value (also I don't want to leave the main page, just to change a variable in the code). I tried some scripts and some functions but no succes so far, all the tutorials on the internet follow a simple boolean for an LED switching ON/OFF . Any explanation about text forms would be appreciated
Thank you in advance!
I have a full functional website for a DHT22 sensor (thermostat) which I made only using simple HTML code and I am trying to improve the webserver communication by replacing the old school HTML with AJAX so whenever I send a command I don't have to refresh the page or change the page. I have to mention that I don't know JavaScript but I'm trying to understand the code.
the simple code I'm using is:
Code: Select all
void handleSave() {
String str = "Settings Saved ...\r\n";
Serial.print("number of arguments ");
Serial.println(webserver.args()); // number of arguments
if (webserver.args() > 0 ) {
for ( uint8_t i = 0; i < webserver.args(); i++ ) {
str += webserver.argName(i) + " = " + webserver.arg(i) + "\r\n";
Serial.println("Arg " + String(i) + "=" + webserver.argName(i));
Serial.println("Arg " + String(i) + "=" + webserver.arg(i));
}
String inString = "";
inString = (webserver.arg(0));
settemp = inString.toFloat();
Serial.print("settemp ");
Serial.println(settemp);
Serial.println();
}
webserver.send(200, "text/plain", str.c_str());
}
the button and textfield form I'm using is:
Code: Select all
page += "<form method='post' action=/save >";
page += "<b>Temperature Setpoint:<br><input name='settemp' type='text' size='16' value='' ></b><br><br> ";
page += "<input type='submit' name='Temperature change' value='Accept'><br><br>";
page += "</form>";
and in void setup I'm using this handling, so when the button is clicked it sends me to the /save page and the value of the variable changes.
Code: Select all
webserver.on("/save", handleSave);
Can someone explain how to implement this with AJAX (GET, SET) so when I complete the text field form with my temp. value and I press on the button there should be no refresh to change the value (also I don't want to leave the main page, just to change a variable in the code). I tried some scripts and some functions but no succes so far, all the tutorials on the internet follow a simple boolean for an LED switching ON/OFF . Any explanation about text forms would be appreciated
Thank you in advance!