nodeMCU Webserver processes input 10 times

I have copy-paste-edited the following code for a webserver to get a input string from a text field for later processing. It works so far and sends the string to serial.print.
But there is one thing i don’t understand: If i click send, the input is printed 10 times, witch takes a couple of seconds.

// Libraries
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>


// WiFi network
const char* ssid     = "myNetwork";
const char* password = "myPassword";

ESP8266WebServer server ( 80 );



char htmlResponse[3000];

void handleRoot() {

  snprintf ( htmlResponse, 3000,
             "<!DOCTYPE html>\
<html lang=\"en\">\
  <head>\
    <meta charset=\"utf-8\">\
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
  </head>\
  <body>\
          <h1>Send Text</h1>\
          <input type='text' name='form_text' id='form_text' size=20 autofocus>\
          <div>\
          
<button id=\"send_button\">send</button>\
          </div>\
    <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>\
    <script>\
      var input_string;\
      $('#send_button').click(function(e){\
        e.preventDefault();\
        input_string = $('#form_text').val();\
        $.get('/send?in_string=' + input_string, function(data){\
          console.log(data);\
        });\
      });\
    </script>\
  </body>\
</html>");

  server.send ( 200, "text/html", htmlResponse );
}


void handleSend() {
  if (server.arg("in_string") != "") {
    Serial.println(server.arg("in_string"));
  }
}


void setup() {

  // Start serial
  Serial.begin(115200);
  delay(10);

  // Connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  server.on ( "/", handleRoot );
  server.on ("/send", handleSend);

  server.begin();
  Serial.println ( "HTTP server started" );
}

void loop() {
  server.handleClient();
}

and the output

.
WiFi connected
IP address: 
192.168.1.106
HTTP server started
hallo
hallo
hallo
hallo
hallo
hallo
hallo
hallo
hallo
hallo

Can someone explain what is going wrong there?

Thank you very much

Lena

Can someone explain what is going wrong there?

First, anonymous printing sucks. You have no easy way to determine why "hallo" was printed. Why not? It is trivial to print an identifier before any piece of text being printed, so you KNOW what part of the code printed that particular piece of text.

Second, do you have a clue what that script does? There does not appear to be any reason to have that script. A form with a submit button will do exactly what you want without the need for any script. WITHOUT using an Arduino, learn EXACTLY what that script is doing.

PaulS:
First, anonymous printing sucks. You have no easy way to determine why “hallo” was printed. Why not? It is trivial to print an identifier before any piece of text being printed, so you KNOW what part of the code printed that particular piece of text.

OK, this was only for testing and there is only one statement, wich can send the response.
For more complex use, i agree.

PaulS:
Second, do you have a clue what that script does? There does not appear to be any reason to have that script. A form with a submit button will do exactly what you want without the need for any script. WITHOUT using an Arduino, learn EXACTLY what that script is doing.

To be honest - no, not realy :confused:
Do you talk about something like this?

<form action="/send" method="get" id="form1">
 Text: <input type="text" name="input">

</form>
<button type="submit" form="form1" value="send">send</button>

As i wrote, this are more or less picked fragments from different examples and you can belive me, i don’t like anything like the imported script in my code.

Greets Lena

Hey PaulS,
thanks a lot for your inspiration!
This simplifies the code very much

 snprintf ( htmlResponse, 3000,
  "<!DOCTYPE html>\
  <html>\
    <head>\
      <meta charset='utf-8'>\
    </head>\
    <body>\
      <h1>Send Text</h1>\
      <form action=/send' method='POST' id='form1'>\
        Text: <input type='text' name='input' size=20 autofocus>
\
      </form>\
      <button type='submit' form='form1' value='send'>send</button>\
    </body>\
  </html>");

  server.send ( 200, "text/html", htmlResponse );
}


void handleSend() {
  if (server.args() > 0 ) {
    for ( uint8_t i = 0; i < server.args(); i++ ) {
      if (server.argName(i) == "input") {
        Serial.print(server.arg(i));
      }
    }
  }
  server.send(200, "text/html", htmlResponse);
}

and it works as expected ;D