在jquery .serialize()之后如何处理php [英] What to do with php after jquery .serialize()
问题描述
key1 = value& key2 = value2& key3 = value3
而即时通讯使用的是发布请求。它看起来很简单,但不知何故,我很难确定如何访问这些键/值对,我无法使用&因为那会给我
[0] => key1 = value1
[1] => key2 = value2
[2] => key3 = value3
我无法使用$ _POST ['key1']或$ _GET ['key1' ]在PHP中访问这些值。我该怎么办!!!谢谢
作为一个侧面问题,我注意到.serilize()用+替换换行符和%空格,以及如何用php解码这些值?再次感谢!
编辑:
jquery代码是相当基础的:
var formSubmit = $(this).serialize();
$ .post('ajax.php',{formSubmit:true,formInfo:formSubmit}
$ b $如果您使用jQuery的Ajax功能提交表单数据,那么使用 .serialize()时应该没有问题。 / code>。服务器应该自动查看并自动解析POST内容。
作为演示,请参阅以下代码:
HTML
< form id =category-dynamicclass = dynamic>
< fieldset id =inner-fieldset>
< legend id =new-category>
< label for =category-name >类别名称:< / label>
< / legend>
< / legend>
< < ul id =category-fields>
< li>
< label>字段#1:< / label>< br />
< input type =textname =fields []value =/>
< / li>
< li>
< label>字段#2:< / label>< br />
< input type =textname =fields []value =/>
< / li>
< / ul>
< / fieldset>
< / form>
< h3> POST结果< / h3>
< pre>< / pre>
jQuery
< $($'$')。$($'$')$($'$')。
$ b $ .post(http://jfcoder.com/test/processor.php,$('#category-dynamic')。serialize(),function(data){
$('pre')。html($('pre')。html()+\\\
\\\
+ data);
});
});
编辑
<处理器.php文件内容:
<?php
print_r($ _ POST);
?>
编辑2
我认为你的错误是你发送的内容是以表单数据为文本字符串而不是url编码的内容。
例如,你可以这样做:
var formSubmit = $(this).serialize()+& formSubmit =真正;
$ .post('ajax.php',formSubmit);
而且你会得到同样的效果,服务器可以扩展你的POST变量事件。
编辑3
请看这个例子:
其中代码为:
$(document).ready(function(){
var serial = $('#category-dynamic')。serialize()+& formSubmit = true;
$('pre')。html(serial);
$ .post(http://jfcoder.com/test/processor.php,serial,function(data){
$('pre')。html($('pre')。html()+ \\\
\\\
+ data);
});
});
请注意添加& formSubmit = true
到串行数据。从PHP页面输出:
POST结果
category-name =& fields%5B %5D =& field%5B%5D =& formSubmit = true
Array
(
[category-name] =>
[fields] = >数组
(
[0] =>
[1] =>
)
[formSubmit] => true
编辑4
这使用您描述的方法。查看区别?
$(document).ready(function(){
var serial = $('# (); $ b $('pre')。html(serial);
$ .post(http://jfcoder.com/test/processor.php, {formSubmit:true,serial:serial},function(data){
$('pre')。html($('pre')。html()+\\\
\\ \\ n+ data);
});
});
OUTPUT
POST结果
category-name =& fields%5B%5D =& fields%5B%5D =
Array
(
[formSubmit] => true
[serial] => category-name =& field%5B%5D =& field%5B%5D =
)
Ok somehow im having the hardest time figuring this out so i want to do an call ajax with a form and im using jquery to serialize it with .serialize(). The data being sent to php looks something like this
key1=value&key2=value2&key3=value3
And im using a post request. It looks simple enough, but somehow im having a really hard time figuring out how to access these key/value pairs, i cant use explode() on & because that will give me
[0] => key1=value1
[1] => key2=value2
[2] => key3=value3
and i cant use $_POST['key1'] or $_GET['key1'] in php to access these values. What should i do!!! Thanks
And as a side question i notice .serilize() replaces line breaks with %0A and white spaces with +, how do i decode these values with php? Thanks again!
Edit:
Hey well the jquery code is fairly basic its :
var formSubmit = $(this).serialize();
$.post('ajax.php',{"formSubmit": "true", "formInfo": formSubmit}
If you're submitting the form data with jQuery's Ajax functionality, there should not be a problem with using .serialize()
. The server should see and urldecode automatically the POST content.
As a demonstration, see this code:
HTML
<form id="category-dynamic" class="dynamic">
<fieldset id="inner-fieldset">
<legend id="new-category">
<label for="category-name">Category Name: </label>
<input type="text" name="category-name" value="" />
</legend>
<ul id="category-fields">
<li>
<label>Field #1:</label><br />
<input type="text" name="fields[]" value="" />
</li>
<li>
<label>Field #2:</label><br />
<input type="text" name="fields[]" value="" />
</li>
</ul>
</fieldset>
</form>
<h3>POST Result</h3>
<pre></pre>
jQuery
$(document).ready(function(){
$('pre').html($('#category-dynamic').serialize());
$.post("http://jfcoder.com/test/processor.php", $('#category-dynamic').serialize(), function(data){
$('pre').html($('pre').html()+"\n\n"+data);
});
});
EDIT
And the processor.php file contents:
<?php
print_r($_POST);
?>
EDIT 2
I think your error is that you're sending the content in such a way as to make the form data be a text string instead of url-encoded content.
For instance, you could do this:
var formSubmit = $(this).serialize() + "&formSubmit=true";
$.post('ajax.php', formSubmit);
And you'd have the same effect, and the server would be able to expand your POST variables without incident.
EDIT 3
See this example:
Where the code is:
$(document).ready(function(){
var serial = $('#category-dynamic').serialize() + "&formSubmit=true";
$('pre').html(serial);
$.post("http://jfcoder.com/test/processor.php", serial, function(data){
$('pre').html($('pre').html()+"\n\n"+data);
});
});
Note the addition of the "&formSubmit=true"
to the serial data. This outputs from the PHP page:
POST Result
category-name=&fields%5B%5D=&fields%5B%5D=&formSubmit=true
Array
(
[category-name] =>
[fields] => Array
(
[0] =>
[1] =>
)
[formSubmit] => true
)
EDIT 4
This uses the method you describe. See the difference?
$(document).ready(function(){
var serial = $('#category-dynamic').serialize();
$('pre').html(serial);
$.post("http://jfcoder.com/test/processor.php", {"formSubmit":"true","serial":serial}, function(data){
$('pre').html($('pre').html()+"\n\n"+data);
});
});
OUTPUT
POST Result
category-name=&fields%5B%5D=&fields%5B%5D=
Array
(
[formSubmit] => true
[serial] => category-name=&fields%5B%5D=&fields%5B%5D=
)
这篇关于在jquery .serialize()之后如何处理php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!