【发布时间】:2012-06-23 19:39:21
【问题描述】:
我已经查看了有关此问题的其他几个建议,但由于某种原因,我的数据未发布。这是我的代码的相关部分:
<input type="button" id="map_submit" value="Map Selection" />
<?php var_dump($_POST); ?> // display the posted variables
和
<script type="text/javascript">
$(document).ready(function(){
$('#map_submit').click(function(){
/* this part is from the slickgrid plugin, in which
* selectedData is an array that I want to post to the page
* and read via php
*/
var selectedData = [], selectedIndexes;
selectedIndexes = grid.getSelectedRows();
jQuery.each(selectedIndexes, function (index, value) {
selectedData.push(grid.getData()[value]);
});
$.post("mapper.php", {dataArray: selectedData});
});
});
</script>
根据我从其他问题中看到的情况,$.post 应该可以工作。但是,当我单击按钮时,var_dump 没有显示任何内容。作为健全性检查,如果我将其添加到 javascript:
for (var i = 0; i < selectedData.length; i++) {
document.write(selectedData[i].questionID + "<br />");
}
它将打印我在网格中选择的questionID 值(当然是新的空白页)。
【问题讨论】:
-
Pass Javascript Array -> PHP 的可能重复项
-
如果你 console.log(selectedData);在 .post 之前?
-
等等,这些代码是在同一个文件中吗?如果是,那么您似乎期望一段 PHP 运行客户端。它不是那样工作的。您需要在
$.post上进行回调以对返回的数据进行处理。 -
是的,给我们看看文件结构和var_dump()在哪里...
-
使用 JSON 将您的 JS 数据编码为字符串,然后使用 PHP 中的
json_decode函数将其解码为 php 对象。
标签: php javascript jquery .post