title: Sending JSON to PHP using ajax
body: <p>I want to send some data in json format to php and do some operation in php. My problem is i can't send json data via ajax to my php file.Please help me how can i do that. I have tried this way..</p>
  
  <pre><code>&lt;script&gt;
  $(function (){
   $("#add-cart").click(function(){
      var bid=$('#bid').val();
      var myqty=new Array()
      var myprice=new Array()
  
      qty1=$('#qty10').val();
      qty2=$('#qty11').val();
      qty3=$('#qty12').val();
  
      price1=$('#price1').val();
      price2=$('#price2').val();
      price3=$('#price3').val();
  
      var postData = 
                  {
                      "bid":bid,
                      "location1":"1","quantity1":qty1,"price1":price1,
                      "location2":"2","quantity2":qty2,"price2":price2,
                      "location3":"3","quantity3":qty3,"price3":price3
                  }
      var dataString = JSON.stringify(postData);
  
      $.ajax({
              type: "POST",
              dataType: "json",
              url: "add_cart.php",
              data: {myData:dataString},
              contentType: "application/json; charset=utf-8",
              success: function(data){
                  alert('Items added');
              },
              error: function(e){
                  console.log(e.message);
              }
      });
  });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>And in PHP i use:</p>
  
  <pre><code>if(isset($_POST['myData'])){
   $obj = json_decode($_POST['myData']);
   //some php operation
  }
  </code></pre>
  
  <p>When in add print_r($_POST) in php file, it shows array(0) {} in firebug.</p>