ESP8266 get value from textbox

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.

I think you need to link your webpage function to the updateParams() by server.on("/classname", HTTP_POST, updateParams); If you can provide full code i could help you more

1 Like

I edited the question to provide all the code you need. The rest of the code in the .ino file is not releated to the problem. If you can help more I would be very grateful.

Are you running the webpage in ESP or somewhere else