title: post values with ajax, php put it in database
body: <p>I want to accept user data from a form, use AJAX to post the values to a PHP script, which will insert them into the database.</p>
  
  <p>JQuery:</p>
  
  <pre><code>$(".add").click(function(){
      var order = {
          Name: $name.val(),
          Drink: $drink.val(),
      }
      $.ajax({
          type: 'POST',
          url: '/ajax/api/orders.php',
          data: order,
          success: function(newOrder){
              $orders.append('&lt;li&gt;Name: ' + newOrder.Name +', Drink: '+ newOrder.Drink +'&lt;/li&gt;')
      }
      })
  });
  </code></pre>
  
  <p>Php:</p>
  
  <pre><code> //open connection to mysql db
  $connection = mysqli_connect("localhost","root","xxxxxxxxx","test") or die("Error " . mysqli_error($connection));
  
  //fetch table rows from mysql db
  $sql = "select * from orders";
  $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
  
  $name = $_POST["Name"];
  $drink = $_POST["Drink"];
  
  $sql2 = "INSERT INTO orders (Id, Name, Drink) VALUES (NULL, '$name', '$drink')";
  //create an array
  $emparray = array();
  while($row =mysqli_fetch_assoc($result))
  {
      $emparray[] = $row;
  }
  echo json_encode($emparray);
  
  //close the db connection
  mysqli_close($connection);
  </code></pre>
  
  <p>can somebody help me with it?</p>