php jquery $.post 空数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/271043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery $.post empty array
提问by Neil
I'm using jQuery to post a form to a php file, simple script to verify user details.
我正在使用 jQuery 将表单发布到 php 文件,这是验证用户详细信息的简单脚本。
var emailval = $("#email").val();
var invoiceIdval = $("#invoiceId").val();
$.post("includes/verify.php",
{invoiceId:invoiceIdval , email:emailval },
function(data) {
//stuff here.
});
PHP Code:
PHP代码:
<?php
print_r($_POST);
?>
I look at the response in firebug, it is an empty array. The array should have at least some value.
我查看了 firebug 中的响应,它是一个空数组。数组至少应该有一些值。
I can not work out why the $_POSTisn't working in the php file. Firebug shows the post to contain the contents posted, email and invoice id, just nothing is actually received in the php file.
我不知道为什么$_POST在 php 文件中不起作用。Firebug 显示帖子包含发布的内容、电子邮件和发票 ID,但实际上在 php 文件中没有收到任何内容。
The form:
表格:
<form method="post" action="<?=$_SERVER['PHP_SELF']; ?>" enctype="application/x-www-form-urlencoded">
Anyone know what its doing?
有谁知道它在做什么?
thanks
谢谢
found this - http://www.bradino.com/php/empty-post-array/
发现这个 - http://www.bradino.com/php/empty-post-array/
that a sensible route to go?
这是一条明智的路线吗?
回答by Owen
$.post()passes data to the underlying $.ajax()call, which sets application/x-www-form-urlencodedby default, so i don't think it's that.
$.post()将数据传递给默认$.ajax()设置的底层调用,application/x-www-form-urlencoded所以我不认为是这样。
can you try this:
你能试试这个吗:
var post = $('#myForm').serialize();
$.post("includes/verify.php", post, function(data) {
alert(data);
});
the serialize()call will grab all the current data in form.myForm.
该serialize()调用将获取form.myForm.
回答by Owen
I got bitten by the same issue, and I find the solution Owen gives not appropriate. You're serializing the object yourself, while jQuery should do that for you. You might as well do a $.get() in that case.
我被同样的问题咬了,我发现欧文给出的解决方案不合适。您自己序列化对象,而 jQuery 应该为您完成。在这种情况下,您还不如执行 $.get() 。
I found out that in my case it was actually a server redirect from /mydir to /mydir/ (with slash) that invalidated the POST array. The request got sent to an index.php within /mydir
我发现在我的情况下,它实际上是服务器从 /mydir 重定向到 /mydir/(带斜杠)使 POST 数组无效。请求被发送到 /mydir 中的 index.php
This was on a local machine, so I couldn't check the HTTP traffic. I would have found out earlier if I would have done that.
这是在本地机器上,所以我无法检查 HTTP 流量。如果我这样做,我会更早发现。
回答by Douglas Mayle
application/x-www-form-urlencoded
There's your answer. It's getting posted, you're just looking for the variables in $_POST array. What you really want is $_REQUEST. Contrary to the name, $_POST contains input variables submitted in the body of the request, regardless of submission method. $_GET contains variables parsed from the query string of the URL. If you just want submitted variables, use the $_REQUEST global.
这就是你的答案。它正在发布,您只是在 $_POST 数组中查找变量。你真正想要的是 $_REQUEST。与名称相反,$_POST 包含在请求正文中提交的输入变量,无论提交方法如何。$_GET 包含从 URL 的查询字符串解析的变量。如果您只想提交变量,请使用 $_REQUEST 全局变量。
If you expect to be receiving file uploads, than you want to create a new array including the contents of $_FILES as well:
如果您希望接收文件上传,那么您还想创建一个包含 $_FILES 内容的新数组:
$arguments = $_REQUEST + $_FILES;
$arguments = $_REQUEST + $_FILES;
回答by dardub
I tried the given function from Owen but got a blank alert as well. Strange but i noticed it would output a query string and return a blank alert. Then i'd submit again and it would alert with correct post values.
我尝试了 Owen 的给定功能,但也收到了空白警报。奇怪,但我注意到它会输出一个查询字符串并返回一个空白警报。然后我会再次提交,它会以正确的帖子值发出警报。
Also had the field names set in the html using the id attribute (which was how it was done in a jquery tutorial I was following). This didn't allow my form fields to serialize. When I switched the id's to name, it solved my problem.
还使用 id 属性在 html 中设置了字段名称(这是在我遵循的 jquery 教程中完成的方式)。这不允许我的表单字段序列化。当我将 id 切换为 name 时,它解决了我的问题。
I ended up going with $.ajax after all that since it did what I was looking for.
毕竟,我最终选择了 $.ajax,因为它做了我想要的。
回答by Josh P
I had a case where I was using jQuery to disable all the inputs (even the hidden ones I wanted) just before using jQuery to submit the form. I changed my jQuery to only disable the "button" type inputs and now the hidden vars are posted when the form is submitted! It seems that if you set a hidden input to disabled its values aren't posted with the form!
我有一个案例,在使用 jQuery 提交表单之前,我使用 jQuery 禁用所有输入(甚至是我想要的隐藏输入)。我将 jQuery 更改为仅禁用“按钮”类型输入,现在提交表单时会发布隐藏的变量!似乎如果您将隐藏输入设置为禁用,其值不会随表单一起发布!
Changed:
更改:
$('input').attr('disabled',true);
to:
到:
$('input[type=button]').attr('disabled',true);

