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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用