从发布Javascript来PHP数组 [英] Post an array from Javascript to PHP

查看:138
本文介绍了从发布Javascript来PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Javascript中的数组我试图传递给PHP。我的数组看起来像这样。

I have an array in Javascript that i am trying to pass to php. my array looks like this.

 Array[0]
   "empNo" : "1347"
   "empName" : "John Doe"  

我建立这个数组从这个JavaScript:

I am building this array from this javascript:

$('input[type=text]').each(function()
{
             if ($(this).attr("value").length>0)
             {
                  param[$(this).attr("id")]=$(this).attr("value");
             }            
});

然后我通过使用数组到PHP

Then I pass the array to php using

$.post("example.php",param)

然后在PHP,当我尝试使用后这样的交互:

Then in php when I try to interact with the post like this:

$emp=$_POST['empNo'];
$name=$_POST['empName'];
echo ($_GET[0]);//this is for testing 

它抛出一个错误,指出 EMPNO和empName的指标没有定义

它还说, 0是一个未定义的偏移。结果
感谢您的帮助。

It also says that 0 is an undefined offset.
Thanks for the help

推荐答案

只是这样做对整个表单。

Instead of looping through and manually building your data to pass just do this for the whole form.

var data = $('form').serialize();

$.post("example.php", data);

然后当你在你的PHP脚本指望你应该有机会。

Then you should have access as you expect in your php script.

BUT 请确认您使用的<输入名称=EXAMPLENAME/> 属性,然后尝试访问他们像这...

BUT make sure you're using the <input name="exampleName" /> attribute and then try to access them like this...

$value = $_POST['exampleName'];

而现在,它看起来像你试图使用的id 而不是名称

这篇关于从发布Javascript来PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆