Passing array from Javascript to PHP via POST - Stack Overflow

I've looked at several other suggestions on this issue but for some reason my data is not being po

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

and

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

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 />");
}

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

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

and

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

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 />");
}

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

Share Improve this question edited Jun 25, 2012 at 16:27 thecodeparadox 87.1k22 gold badges142 silver badges164 bronze badges asked Jun 21, 2012 at 14:26 AaronAaron 1,7434 gold badges27 silver badges41 bronze badges 14
  • possible duplicate of Pass Javascript Array -> PHP – Diodeus - James MacFarlane Commented Jun 21, 2012 at 14:29
  • 1 What shows up if you console.log(selectedData); before the .post? – somedev Commented Jun 21, 2012 at 14:29
  • 2 Wait, are these pieces of code in the same file? If they are then it appears you're expecting a piece of PHP to run client side. It doesn't work that way. You need a callback on $.post to do something with the returned data. – Cfreak Commented Jun 21, 2012 at 14:31
  • Yep, show us the file structure and where is var_dump()... – Cranio Commented Jun 21, 2012 at 14:33
  • Use JSON to encode your JS data in strings, and then use json_decode function in PHP to decode it into php objects. – Hrishikesh Commented Jun 21, 2012 at 14:37
 |  Show 9 more ments

2 Answers 2

Reset to default 2
$.post("mapper.php", {dataArray: selectedData});

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.

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.

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
});

You'll have to serialize the object pre-post and then deserialize on the server (presumably in PHP) before you can use it.

Example below (requires json2.js):

<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", JSON.stringify({dataArray: selectedData}));
        });
    });
</script>

<input type="button" id="map_submit" value="Map Selection" />
<?php var_dump(json_decode($_POST, true)); ?>  // display the posted variables

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745294480a4621056.html

相关推荐

  • Passing array from Javascript to PHP via POST - Stack Overflow

    I've looked at several other suggestions on this issue but for some reason my data is not being po

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信