I am trying to get the value from HTML textbox
the code of the ESP:
String IpToPing = "8.8.8.89";
void updateParams(){
Serial.println(IpToPing);
IpToPing = server.arg("IpToPing");
Serial.println(IpToPing);
server.send(200, "text/plain", "updateParams");
server.sendHeader("Location","/");
server.send(303);
}
void setup() {
server.on("/save", updateParams);
The webpage code:
<html>
<head>
<title>Wifi Relay</title>
<script>
function sendCommand(command) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + command, true);
xhr.send();
}
</script>
</head>
<body>
<label for="Name">IP to PING:</label>
<input type="text" id="IP" name="nameIP" placeholder="valid IP(8.8.8.8)">
<button id="saveButton" onclick="sendCommand('save')">Save</button>
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\">
<script>
var IpToPing;
$('#saveButton').click(function(e){
e.preventDefault();
IpToPing = $('#IP').val();
$.get('/save?IpToPing=' + IpToPing, function(data)){
console.log(data);
});
</script>
<a href="/">
<button>Back to homepage</button>
</a>
</body>
</html>
The problem is that when you press the save button the value is just empty, though there is something written in the textbox. the output of Serial.println(IpToPing); is: 8.8.8.89 and then when IpToPing = server.arg("IpToPing"); is called IpToPing becomes "" an empty string so it prints out an empty string and when the save button is pressed again it just printd two times the empty string and so on.