title: submit form HTML using ajax and get json response
body: <p>I am not able to insert data through ajax, I want to insert it and get JSON response too.Actually at the end i want to create an API of this total code.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  error_reporting(1);
  mysql_connect("localhost","root","");
  
  mysql_select_db("behindtherock");
  
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;form &gt;
  &lt;label&gt;Country&lt;/label&gt;&lt;br /&gt;
  &lt;input type="text" id="country" /&gt;
  &lt;br /&gt;
  &lt;label&gt;First name&lt;/label&gt;&lt;br /&gt;
  
  &lt;input type="text" id="first_name" /&gt;
  &lt;br /&gt;
  &lt;label&gt;Last Name&lt;/label&gt;&lt;br /&gt;
  &lt;input type="text" id="last_name" /&gt;
  &lt;br /&gt;
  
  &lt;input type="submit"  id="save" value="post" /&gt;
  &lt;/form&gt;
  
  
  
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
  
  $("#save").click(function(){
  var country=$("#country").val();
  var first_name=$("#first_name").val();
  var last_name=$("#last_name").val();
  var address_line_1=$("#address_line_1").val();
  var address_line_2=$("#address_line_2").val();
  var city=$("#city").val();
  var state=$("#state").val();
  var zip=$("#zip").val();
  var user_phone=$("#user_phone").val();
  var user_email=$("#user_email").val();
  var u_stream=$("#u_stream").val();
  var referring_member=$("#referring_member").val();
  var promotion_code=$("#promotion_code").val();
  $.ajax({
  url:"add.php",
  type:"post",
  async:false,
  data:{
  "done":1,
  "country":country,
  "first_name":first_name,
  "last_name":last_name,
  "address_line_1":address_line_1,
  "address_line_2":address_line_2,
  "city":city,
  "state":state,
  "zip":zip,
  "user_phone":user_phone,
  "user_email":user_email,
  "u_stream":u_stream,
  "referring_member":referring_member,
  "promotion_code":promotion_code,
  
  };
  
  success:function(data){
  
  }
  });
  });
  
  });
  &lt;/script&gt;
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  error_reporting(1);
  include("db.php");
  
  
  if(isser($_POST['done'])){
    $country=mysql_escape_string($_POST['country']);
  $first_name=mysql_escape_string($_POST['first_name']);
  
  $last_name=mysql_escape_string($_POST['last_name']);
  $address_line_1=mysql_escape_string($_POST['address_line_1']);
  $address_line_2=(int)$_POST['address_line_2'];
  $city=mysql_escape_string($_POST['city']);
  $state=mysql_escape_string($_POST['state']);
  $zip=mysql_escape_string($_POST['zip']);
  $user_phone=mysql_escape_string($_POST['user_phone']);
  $user_email=mysql_escape_string($_POST['user_email']);
  $u_stream=mysql_escape_string($_POST['u_stream']);
  $referring_member=mysql_escape_string($_POST['referring_member']);
  $promotion_code=mysql_escape_string($_POST['promotion_code']);
  
  mysql_query("INSERT INTO user_details(country,first_name,last_name,address_line_1,address_line_2,city,state,zip,user_phone,user_email,u_stream,referring_member,promotion_code) 
   VALUES ('('', '$country','$first_name','$last_name','$address_line_1','$address_line_2','$city','$state','$zip','$user_phone','$user_email','$u_stream','$referring_member','$promotion_code')')");
   
  
  
  exit();
  
  }
  
  
  
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am not able to insert data through ajax, I want to insert it and get JSON response too.Actually at the end i want to create an API of this total code.</p>