title: Java Script. Insert data to database by clicking button
body: <p>I need simple thing, insert data to database by clicking button, but I can't do It successfully.</p>
  
  <p>This is button.php</p>
  
  <pre><code>&lt;form method="post" action="" onsubmit="return display()"&gt;
  &lt;input type="text" name="name" id="name22" placeholder=""&gt;
  &lt;input type="submit" name="submit" value="Send"&gt;&lt;br&gt;
  
  &lt;div id="answer22" class="answer"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>This is function which get word which user enter and I need to write It to database.</p>
  
  <pre><code>function display(){
  var i;
  var d = new Date();
  var n = d.getDate();
      for (i=1;i&lt;=30;i++) {
        if(i==n) {
   var ans    =   document.getElementById("name"+ i).value
   document.getElementById("answer"+ i).innerHTML="Jūsų atsakymas: "+ans.bold();
      var ajax = new XMLHttpRequest(),
  url = 'button_sql.php?answer=' + encodeURIComponent(ans);
  ajax.open("GET", url, true);
  ajax.send();
      }
      }
  return false;
  }
  </code></pre>
  
  <p>This is my db.php I don't know how to use It correctly.</p>
  
  <pre><code>&lt;?php
      session_start();
      $name = John; 
      $answer= $_SESSION['answer22']; // I've tried this but unsuccessful
      $mysqli = new mysqli("localhost","asd","asd","asd");
  
  
  if ($stmt = $mysqli-&gt;prepare("INSERT into myTable (answer, userName) VALUE (?,?) ")) {
  if (!$mysqli-&gt;set_charset("utf8")) {
      printf("Error loading character set utf8: %s\n", $mysqli-&gt;error);
  } else {
  //    printf(" ", $mysqli-&gt;character_set_name());
  }
  
  
  $stmt-&gt;bind_param('ss', $answer, $name);
  
     $stmt-&gt;execute();
  
     if ($stmt-&gt;error != '') {
         echo ' error:'.$stmt-&gt;error;
     } else {
         echo 'success';
     }
     $stmt-&gt;close();
  } else {
     echo 'error:'.$mysqli-&gt;error;
  }
  
  $mysqli-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>I've tried use this, but useless:</p>
  
  <p><code>&lt;form method="post" action="button_sql.php" onsubmit="return display()"&gt;</code></p>
  
  <p>So someone could help me? Thank you very much.</p>