weixin_33704234 2012-06-17 13:21 采纳率: 0%
浏览 84

将js数组传递给PHP

Why can't I access my array through $_POST in PHP? I'm trying to use the jQuery $.post method. Here is the corrected code with your suggestions:

My javascript:

<script type="text/javascript">
var selectedValues;
var serializedValues;
$("td").click(function() {
$(this).toggleClass('selectedBox');

// map text of tds to selectedValues
selectedValues = $.map($("td.selectedBox"), function(obj) {
        return $(obj).text();

});

serializedValues = JSON.stringify(selectedValues);

// $.post('/url/to/page', {'someKeyName': variableName}); //exemple
$.post('handler.php', 
      {'serializedValues' : serializedValues}, 
      function(data) {
        //debug 
     }
);

});

</script>

My php:

<?php
if(isset($_POST['serializedValues'])) {

            var_dump($_POST['serializedValues']);
            $originalValues = json_decode($_POST['serializedValues'], 1);
            print_r($originalValues);

        }


?>
  • 写回答

2条回答 默认 最新

  • weixin_33699914 2012-06-17 13:27
    关注

    You should serialize your array into json string:

    serializedValues = JSON.stringify(selectedValues)
    

    And pass it to php. And then decode with json_decode:

    $originalValues = json_decode($_POST['serializedValues'], 1);
    

    http://php.net/manual/ru/function.json-decode.php

    评论

报告相同问题?

悬赏问题

  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析