du3669 2015-05-14 09:05
浏览 63
已采纳

Json在JS中进行字符串化并在php中解码

js

var sim_list=[];
var simvalue = $('input[name="simnamecbx"]:checked').each(function(){
    var sim_name=this.value.split(" ")[0];
    console.log("simname:",sim_name);
    sim_list.push({
        sim_name
    });
);
console.log(sim_list);
var simulation_list = JSON.stringify(sim_list);     
$.ajax({ url: 'addprogram.php',
     data: {addpgmname:addprogname, addpgmcomp:addprogcomp, addpgmdate:addprogdate, simselvalue:simulation_list},
     type: 'post',
     dataType: 'json',
     success: function(output){
         var a = output;
         console.log(a);
     }
});

php

$simvalue = json_decode($_POST["simselvalue"],true);
foreach($simvalue as $key => $value) {
    print_r($value);
}

Question

When I execute the above php I get the values as

Array
(
    [0] => Array
        (
            [sim_name] => 12
        )

    [1] => Array
        (
            [sim_name] => 13
        )

    [2] => Array
        (
            [sim_name] => 14
        )

)

But I want the result to be like 12, 13, 14 which can be inserted into mysql table. Please explain how to parse this array that I get from javascript.

  • 写回答

2条回答 默认 最新

  • doudao5287 2015-05-14 10:21
    关注

    I think that the problem is that you are pushing an object with sim_name on to your array instead of the actual value. This actually only works in ES6 compatible browsers which makes your error invisible.

    The problem is this part in your javascript code:

    sim_list.push({
        sim_name
    });
    

    In ECMAScript 6 this is the equivalent of the following:

    sim_list.push({
        sim_name: sim_name
    });
    

    Thus you are pushing an object with the sim_name property set to the number (e.g. 12, 13, 14).

    What I think that you want to do is the following:

    sim_list.push(sim_name)
    

    This will push only the number onto the array.

    All together now:

    JS

    var sim_list = []
    
    $('input[name="simnamecbx"]:checked').each(function () {
      var sim_name = this.value.split(' ')[0]
      sim_list.push(sim_name)
    })
    
    var data = {
      addpgmname: addprogname,
      addpgmcomp: addprogcomp,
      addpgmdate: addprogdate,
      simselvalue: JSON.stringify(sim_list)
    }
    
    $.ajax({
      url: 'addprogram.php',
      data: data,
      type: 'post',
      dataType: 'json',
      success: function (output) {
        console.log(output)
      }
    })
    

    PHP

    $simvalue = json_decode($_POST["simselvalue"]);
    
    print("The selected values was: " . implode($simvalue, ", "));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探