Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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:

  <form name="drugForm" onsubmit="return validateForm()" method="post">
Drug name: <input type="text" name="dname"></pre>
  <pre>

<input type="checkbox" name="drug" value="one">1    <input type="checkbox" name="drug" value="two">2</br>
<input type="checkbox" name="drug" value="three">3  <input type="checkbox" name="drug" value="four">4</br></br>
    <input type="submit" value="Submit">
  </pre>

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:

 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<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;
  }
 }

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:

 <body>
 <?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";
 ?>
 </body>

Please guide me in this issue, how to pass json array to PHP file using javascript.

share|improve this question
add comment

3 Answers

You have the following lines confused:

 $json = $_POST['json'];
 $array=json_decode($_POST[$json]);

Change to:

 $array=json_decode($_POST['json']);
share|improve this answer
 
I had done that too, but still it is and was the same. –  gree_tejas Nov 19 '13 at 4:51
add comment

Can you try this and check whether you getting the post values here,

 $json = $_POST['json'];
 echo "<pre>"; print_r($json);echo "</pre>";
share|improve this answer
add comment

Try this and see if it works for you

$array = json_decode($_POST['json'], true);
share|improve this answer
 
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. –  joshua Nov 19 '13 at 5:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.