如何将数组值从jquery-ajax formData获取到php

时间:2019-07-07 13:49:59

标签: php jquery ajax

我正在尝试使用jquery-Ajax formData将其值与其他输入字段放入数组的输入字段传递到PHP中,除我在成功传递数组值方面遇到问题外,其他一切似乎都正常运行我尝试了很多,但没有取得明显的成功。

首先,我尝试了SerialiseArray()方法。这是我的下面的代码

 <form>
 //other input field below...
  .
  .
  .
//this is the code to include my array which is in _categories-list.php
<div class="form-group">
<label for="artist">Select Categories </label>
<?php include('../_categories-list.php') ?>
</div>    </form>

  var Category = $('#categoriesList').serializeArray();
  $.each( Category,function(i,field){
  formData.append('categoriesList', field.value + "");
                               });
   $('.msg').text('Uploading in progress...');
         ajaxcall =       $.ajax({
                    url: 'page-videoFunc.php',
                    data: formData, 
                    processData: false,
                    contentType: false,
                    type: 'POST',});

我使用的这种特定方法仅发送数组中所选选项的一个值。例如:

//output: let's say the person chooses blues, hip-hop
hip-hop //will be the only value sent

我还尝试了另一种类似的方法

<form>
 //other input field below...
  .
  .
  .
//this is the code to include my array which is in _categories-list.php
<div class="form-group">
<label for="artist">Select Categories </label>
<?php include('../_categories-list.php') ?>
</div>  </form>

  var Category = $('#categoriesList').serializeArray();
  formData.append('categoriesList', Category);//note that code changes here from the above method used
   $('.msg').text('Uploading in progress...');
         ajaxcall =       $.ajax({
                    url: 'page-videoFunc.php',
                    data: formData, 
                    processData: false,
                    contentType: false,
                    type: 'POST',});

这发送选择的但已发送但作为对象示例的所有数组值:

//output
[object object] [object object]

最后,我尝试了这个:serialize();

<form>
 //other input field below...
  .
  .
  .

//this is the code to include my array which is in _categories-list.php
<div class="form-group">
<label for="artist">Select Categories </label>
<?php include('../_categories-list.php') ?>
</div>  </form>

  var Category = $('#categoriesList').serialize(); //Note i used just serialize() here
  formData.append('categoriesList', Category);
   $('.msg').text('Uploading in progress...');
         ajaxcall =       $.ajax({
                    url: 'page-videoFunc.php',
                    data: formData, 
                    processData: false,
                    contentType: false,
                    type: 'POST',});

部分工作并发送所有值,但格式似乎无法获取这些值,例如:

 //output
 categoriesList%5B%5D=blues&categoriesList%5B%5D=hip-hop

我不知道如何通过这种方法仅从查询字符串中获取值,所以我可以将其放入数据库中

请帮助我为我使用的上述任何方法提供解决方案,我已经完成了将近42个小时的工作,并且它拖慢了我的项目进度

2 个答案:

答案 0 :(得分:1)

像这样调用ajax。

df1 = (df.melt('postal_code', value_name='name')
         .drop('variable', axis=1)
         .dropna(subset=['name'])
         .sort_values('postal_code')
         .reset_index( drop=True)

)
print (df1)
   postal_code    name
0         1020  Pulset
1         1020  Cmotor
2         1310  Rotaxi
3         1310   Datec
4         1410  Nitron
5         1410  Rotory
6         1410   Datec

在page-videoFunc.php文件中,使用parse_str解析form_data。

var Category = $('#categoriesList').serialize();
$.ajax({
                    url: 'page-videoFunc.php',
                    type: 'post',
                    data:{
                         action:'update_data',
                         form_data:Category 
                     },

});

答案 1 :(得分:0)

在使用parse_str切断添加到序列化数据的URL之后,要获取数组的所有值,您应该这样做:

 parse_str($_POST['name'], $output);
  $x = $output["name"];
  foreach ($x as $key => $value) {
   echo $value;
      }