javascript - Json stringify in JS and decoding in 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 = output; console.log(a); } });
php
$simvalue = json_decode($_post["simselvalue"],true); foreach($simvalue $key => $value) { print_r($value); }
question
when execute above php values
array ( [0] => array ( [sim_name] => 12 ) [1] => array ( [sim_name] => 13 ) [2] => array ( [sim_name] => 14 ) )
but want result 12, 13, 14 can inserted mysql table. please explain how parse array javascript.
i think problem pushing object sim_name
on array instead of actual value. works in es6 compatible browsers makes error invisible.
the problem part in javascript code:
sim_list.push({ sim_name });
in ecmascript 6 equivalent of following:
sim_list.push({ sim_name: sim_name });
thus pushing object sim_name
property set number (e.g. 12, 13, 14).
what think want following:
sim_list.push(sim_name)
this push number onto array.
all 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, ", "));
Comments
Post a Comment