php用parse_str方法拆分jQuery.serialize()打包提交的数据

这篇文章记录用。
html code:

[code lang=”html”]
<!DOCTYPE html>
<html>
<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form action="" id="searchForm">
请输入一个数字,测试<input type="number" name="s" placeholder="Enter a number…" /> +
<input type="number" name="b" placeholder="Enter a number…" />
<input type="number" name="b1" placeholder="Enter a number…" />
<input type="number" name="b2" placeholder="Enter a number…" />
<input type="number" name="b3" placeholder="Enter a number…" />
<input type="number" name="b4" placeholder="Enter a number…" />
<input type="number" name="b5" placeholder="Enter a number…" />
<input type="button" value="POST" id="su" />
</form>
<!– the result of the search will be rendered inside this div –>
<div id="result"></div>

<script>
$(function(){
$("#su").click(function() {
var form = $(‘#searchForm’).serialize();
$.post("test.php",{data:form},function(data){$("#result").html(data);});
// alert("fsdf");
});
});
</script>

</body>
</html>
[/code]

php code

[code lang=”php”]
<?php
var_dump($_POST);

$ss = $_POST[‘data’];
echo ‘<Br>获取提交的数据:<Br>’;
echo $ss;

echo ‘<Br>转换成字符输出:<Br>’;
//echo $ss;
parse_str($ss,$op);
echo parse_str($ss);
echo $b4;
echo ‘<Br>转换成数组输出:<Br>’;
echo $op[‘s’].$op[‘b’].$op[‘b2’];
//echo $op[‘b’]
?>

[/code]

输出:

提交的数据:
array(1) { [“data”]=> string(63) “s=1111&b=222&b1=11&b2=1212&b3=3242354&b4=6457568fsd&b5=79870890” }
获取提交的数据:
s=1111&b=222&b1=11&b2=1212&b3=3242354&b4=6457568fsd&b5=79870890
转换成字符输出:
6457568fsd
转换成数组输出:
11112221212