从AJAX接收serializeArray数据到处理php页面

时间:2015-10-20 09:23:57

标签: javascript php jquery html json

$('#submit').click(function()
{
var data = JSON.stringify($("#players_form").serializeArray());
alert(data);
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
        type : 'POST',
        data : data,
        url  : 'process.php',
        success: function(responseText)
       {
            if(responseText == 1)
            {
            alert("Sucess");
            }
        }

在这段代码中我将表单值传递给process.php但是我无法在process.php中接收发布的值,是否有人建议我在process.php中接收数组值

<form id="players_form"  >
<input type=" text" name="main_name[]" value="">
<input type=" text" name="main_name[]" value="">
<input type=" text" name="main_name[]" value="">
<input type=" text" name="main_name[]" value="">
<input type=" text" name="main_name[]" value="">
<input type=" text" name="sub_name[]" value="">
<input type=" text" name="sub_name[]" value="">
<input type=" text" name="sub_name[]" value="">
<input type=" text" name="sub_name[]" value="">
<input type=" text" name="sub_name[]" value="">
<input type="button" value="Submit" id="submit">
</form>

3 个答案:

答案 0 :(得分:0)

您可以以两种不同的方式使用serializeArray方法。 var data = JSON.stringify($("form").serializeArray());

var data = JSON.stringify($(":input").serializeArray());

答案 1 :(得分:0)

试试这个ajax脚本,我确保它有效。

&#13;
&#13;
$(document).ready(function() {
  $('#submit').click(function() {
    var post_data = $("#players_form").serializeArray();
    $.ajax({
      type: 'POST',
      data: post_data,
      url: 'process.php',
      success: function(responseText) {
        console.log(responseText);
        if (responseText == 1) {
          alert("Sucess");
        }
      }
    })
  })
});
&#13;
&#13;
&#13;

process.php 上获取此值

$main_name = $_POST['main_name'];
$sub_name = $_POST['sub_name'];
$main_name_version = implode(',', $main_name);
$sub_name_version = implode(',', $sub_name);
echo $main_name_version;
echo $sub_name_version;

答案 2 :(得分:0)

替换数据,

 var data = $("#players_form").serializeArray();

将您的请求设为,

dataType : 'JSON',
data : {players:data},

并在服务器中处理数据,如下所示,

$player = $_POST['players'];