通过POST将数组从Javascript传递到PHP [英] Passing array from Javascript to PHP via POST

查看:101
本文介绍了通过POST将数组从Javascript传递到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了有关此问题的其他一些建议,但由于某些原因,我的数据未发布.这是我的代码的相关部分:

I've looked at several other suggestions on this issue but for some reason my data is not being posted. Here is the relevant section of my code:

<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中:

From what I've seen from other questions, $.post should work. But, when I click the button, nothing is shown from the var_dump. As a sanity check, if I add this to the javascript:

for (var i = 0; i < selectedData.length; i++) {
     document.write(selectedData[i].questionID + "<br />");
}

它将打印我在网格中选择的questionID值(当然到新的空白页).

it will print the questionID values I selected in the grid (of course to a newly blank page).

推荐答案

$.post("mapper.php", {dataArray: selectedData});

这行很好.我不知道为什么每个人都建议使用JSON,因为这里没有必要.您可以正常使用JSON来发布对象/数组.

This line is fine. I don't know why everyone is suggesting JSON, because that's unnecessary here. You can just POST objects/arrays normally without using JSON.

注意:这将重新加载页面.它将通过AJAX发布到mapper.php,因此不会在页面上的任何地方看到任何内容.您需要查看自己的开发工具,看看AJAX调用返回了什么.

Note: this will not reload the page. It will POST to mapper.php via AJAX, so aren't going to see anything anywhere on the page. You'd need to look in your dev tools to see what the AJAX call returned.

或者,您可以检查POST返回的数据.

Or, you can check the data the POST returns.

$.post("mapper.php", {dataArray: selectedData}, function(data){
    console.log(data); // check your console, you should see some output
});

这篇关于通过POST将数组从Javascript传递到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆