title: Submit a form to MySQL using AJAX
body: <p>I try to submit a form to my MySQL DB, using jQuery (AJAX). When I press the submit button, I get the failed message "failed" I wrote in JavaScript. I think there is something wrong in my JavaScript syntax. </p>
  
  <p><strong>HTML/JavaScript</strong></p>
  
  <pre><code>&lt;form method="post" action="opslaan.php"&gt;
    &lt;input type="text" id="itmnlid" name="tmnlid"&gt;
    &lt;input type="text" id="ibesproken" name="besproken"&gt;
    &lt;input type="text" id="iafspraken" name="afspraken"&gt;
    &lt;button type="submit"&gt;Save&lt;/button&gt;
    &lt;p id="result"&gt;&lt;/p&gt;
  &lt;/form&gt;
  
  &lt;script&gt;
    $("form").submit(function(e) {
      e.preventDefault();
      $.post(
        'opslaan.php', {
          id: $("#itmnlid").val(),
          besproken: $("#ibesproken").val(),
          afspraken: $("#iafspraken").val()
        },
        function(result) {
          if (result == "succes") {
            $("#result").html("succes");
          } else {
            $("#result").html("failed");
          }
        }
      );
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>$id = $_POST["id"] ;
  $besproken = $_POST["besproken"];
  $afspraken = $_POST["afspraken"];
  
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "INSERT INTO coaching (id, besproken, afspraken)
  VALUES ('$id', '$besproken', '$afspraken')";
  
  if ($conn-&gt;query($sql) === TRUE) {
      echo "succes";
  } else {
      echo "Error: " . $sql . "&lt;br&gt;" . $conn-&gt;error;
  }
  
  $conn-&gt;close();
  ?&gt;
  </code></pre>