JSON不将数组数据传递给PHP [英] JSON not passing array data to PHP
问题描述
我正在尝试将我的代码更改为在上一个问题的推荐下使用json来简化事情...
I am trying to change my code to use json on recommendation from a previous question to simplify things a bit...
在客户端:
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../js/jquery.tablednd_0_5.js"></script>
<script type="text/javascript" src="../js/jquery.json-2.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#table').tableDnD();
});
function sendData() {
data = $('#table').tableDnDSerialize();
alert(data); // shows expected data
document.dataform.data.value = $.toJson(data);
document.data.submit();
}
</script>
<form action="$php_page_name" method="post" name="dataform" onSubmit="sendData()">
<input type="hidden" name="data" />
<input type="submit" value="Submit" />
</form>
js警报输出预期的数组,我认为到此为止它会转换为字符串.但是当我提交form.data时,我的php:
The js alert outputs the expected array, which i think is converted to a string by this point. But when I submit form.data, my php:
$data = json_decode($_POST['data']);
print_r($data);
print_r($_POST);
仅返回:
Array([data] =>)
Array ( [data] => )
有什么想法为什么没有通过?
Any ideas why nothing is being passed ?
干杯, 安迪
推荐答案
您需要等待函数的返回:
You need to wait for the return of the function:
onSubmit="return sendData()"
否则,表单将立即提交,并且不会等到数据更改后.
Otherwise the form will be submitted immediatly and does'nt wait till data is changed.
在函数内部替换
document.data.submit();
与此:
return true;
此外:假设您将其用作jquery.json-2.2.js:
http: //code.google.com/p/jquery-json/downloads/detail?name=jquery.json-2.2.js&can=2&q=
方法名称为
$.toJSON
代替$.toJson
Furthermore: assuming you use this as jquery.json-2.2.js :
http://code.google.com/p/jquery-json/downloads/detail?name=jquery.json-2.2.js&can=2&q=
The method-name is
$.toJSON
instead of $.toJson
这篇关于JSON不将数组数据传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!