title: Passing of JSON array to PHP file using javascript AJAX function
body: <p>I am now about to give up for ajax in javascript to pass JSON array to PHP. I have first PHP file in which I have form which contains a text area and checkboxes. The snippet is below:</p>
  
  <pre><code>  &lt;form name="drugForm" onsubmit="return validateForm()" method="post"&gt;
  Drug name: &lt;input type="text" name="dname"&gt;&lt;/pre&gt;
    &lt;pre&gt;
  
  &lt;input type="checkbox" name="drug" value="one"&gt;1    &lt;input type="checkbox" name="drug" value="two"&gt;2&lt;/br&gt;
  &lt;input type="checkbox" name="drug" value="three"&gt;3  &lt;input type="checkbox" name="drug" value="four"&gt;4&lt;/br&gt;&lt;/br&gt;
      &lt;input type="submit" value="Submit"&gt;
    &lt;/pre&gt;
  </code></pre>
  
  <p>Here, with validateForm() call I am calling javascript to check whether text area is filled and at least a checkbox is checked. Also, it is creating array, in javascript, to get checked values. Here with this js I want to send this array to PHP file by converting it JSON using ajax. COde snippet is below:</p>
  
  <pre><code> function validateForm()
   {
    var x=document.forms["drugForm"]["dname"].value;
    var y=document.drugForm.drug;
    var y_array = new Array();
  
     if (x==null || x=="")
     {
  alert("First name must be filled out");
  return false;
     }
  
     else if (Boolean(x))
     {
   for(k=0;k&lt;y.length;k++)
   {
      if(y[k].checked)
      {
          var arr_val = y[k].value;
          y_array.push(arr_val);
          //alert(arr_val);
      }
  
  }
     $.ajax({
      type: "POST",
      url: "drug_form3.php",
      dataType: 'json',
      data: {json: JSON.stringify(y_array)},
          });
  
  alert("Check one checkbox at least");
  return false;
    }
   }
  </code></pre>
  
  <p>Here, with js array is getting created and whenever I am printing values with alert, it is giving proper result. But, when I am trying to send it to next PHP file even after using json_decode in php file, it is not able to get array printed with PHP. Below is code snippet for second PHP:</p>
  
  <pre><code> &lt;body&gt;
   &lt;?php
   $json = $_POST['json'];
   $array=json_decode($_POST[$json]);
  
    // here i would like use foreach:
  
    print_r ($array);
    echo "Its in form2 and working fine";
   ?&gt;
   &lt;/body&gt;
  </code></pre>
  
  <p>Please guide me in this issue, how to pass json array to PHP file using javascript.</p>