简体   繁体   English

使用javascript AJAX函数将JSON数组传递到PHP文件

[英]Passing of JSON array to PHP file using javascript AJAX function

I am now about to give up for ajax in javascript to pass JSON array to PHP. 我现在要放弃将JSON数组传递给PHP的javascript中的ajax。 I have first PHP file in which I have form which contains a text area and checkboxes. 我有第一个PHP文件,其中有包含文本区域和复选框的表单。 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. 在这里,通过validateForm()调用,我正在调用javascript以检查文本区域是否已填充,并且至少选中了一个复选框。 Also, it is creating array, in javascript, to get checked values. 另外,它正在用javascript创建数组以获取检查值。 Here with this js I want to send this array to PHP file by converting it JSON using ajax. 在此使用此js,我想通过使用ajax将其转换为JSON将此数组发送到PHP文件。 COde snippet is below: 以下是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<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. 在这里,随着js数组的创建,每当我打印带有警报的值时,它就会给出正确的结果。 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. 但是,即使在php文件中使用json_decode后,即使尝试将其发送到下一个PHP文件,也无法使用PHP打印数组。 Below is code snippet for second PHP: 以下是第二个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. 请在这个问题上指导我,如何使用javascript将json数组传递到PHP文件。

You have the following lines confused: 您对以下几行感到困惑:

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

Change to: 改成:

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

Can you try this and check whether you getting the post values here, 您可以尝试一下并检查您是否在此处获得帖子值吗?

 $json = $_POST['json'];
 echo "<pre>"; print_r($json);echo "</pre>";

试试这个,看看是否适合您

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM