Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在jquery中创建关联数组_Php_Jquery_Arrays_Ajax - Fatal编程技术网

Php 在jquery中创建关联数组

Php 在jquery中创建关联数组,php,jquery,arrays,ajax,Php,Jquery,Arrays,Ajax,我需要创建一个关联数组,用于将与ajax post相同的内容发送到php脚本。我该怎么做 更具体地说,我有两行输入,分别名为“firstname”和“lastname”。当我按下submit按钮时,将生成如下数组: var name_array = [{"first_name" : string1, "last_name" : string2}, {"first_name" : string3, "last_name" : string4}, . . .] 然后,我将通过ajax po

我需要创建一个关联数组,用于将与ajax post相同的内容发送到php脚本。我该怎么做

更具体地说,我有两行输入,分别名为“firstname”和“lastname”。当我按下submit按钮时,将生成如下数组:

var name_array = [{"first_name" : string1, "last_name" : string2},
 {"first_name" : string3, "last_name" : string4},
 .
 .
 .]
然后,我将通过ajax post将此数组发送到php。如果进行了上述构造,那么将名称数组作为数据发送到php脚本的语法应该是什么?


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function()
{
    var hold_data = {};
    $(document).on('click','#submit',function()
    {
        $("#myform .field").each(function()
        {
            hold_data[$(this).attr('name')] = $(this).val();
         });
        var json_data = JSON.stringify(hold_data);
        alert(json_data);

         $.ajax(
         {
             url:'', //url location for where you want to submit the data
             type:'post',
             data:{form_data:json_data},
             success:function(res)
             {
                 alert("Successfully sent and your response "+res);
              }
          });
      }); 
});
</script>
</head>

<body>
<form id="myform" action="">
  First name: <input type="text" class="field" name="FirstName"  value="Tom"><br>
  Last name: <input type="text" class="field" name="LastName" value="Raze"><br>
  <input type="button" id="submit" value="Submit">
</form>
</body>
$(文档).ready(函数() { var hold_data={}; $(文档)。在('单击','提交',函数()上) { $(“#myform.field”)。每个(函数() { hold_data[$(this.attr('name')]=$(this.val(); }); var json_data=json.stringify(hold_data); 警报(json_数据); $.ajax( { url:“”,//要提交数据的url位置 类型:'post', 数据:{form_data:json_data}, 成功:功能(res) { 警报(“成功发送且您的响应”+res); } }); }); }); 名字:
姓氏:
如果您正在发布到php页面,那么您可以解码json数据,如下所示

回送的温度值将在ajax中显示为响应res数据

<?php

if(isset($_POST['form_data']))
{
    $temp = json_decode($_POST['form_data']);
    echo temp;
}

?>


$(文档).ready(函数()
{
var hold_data={};
$(文档)。在('单击','提交',函数()上)
{
$(“#myform.field”)。每个(函数()
{
hold_data[$(this.attr('name')]=$(this.val();
});
var json_data=json.stringify(hold_data);
警报(json_数据);
$.ajax(
{
url:“”,//要提交数据的url位置
类型:'post',
数据:{form_data:json_data},
成功:功能(res)
{
警报(“成功发送且您的响应”+res);
}
});
}); 
});
名字:
姓氏:
如果您正在发布到php页面,那么您可以解码json数据,如下所示

回送的温度值将在ajax中显示为响应res数据

<?php

if(isset($_POST['form_data']))
{
    $temp = json_decode($_POST['form_data']);
    echo temp;
}

?>


使用jQuery时可能会出现重复的情况,对吗?使用jQuery时可能会出现重复的情况,对吗?非常感谢你。这工作正是我想要的。再次感谢威尔森非常感谢你。这工作正是我想要的。再次感谢你,威尔逊