<?
$cl = $row["saved_json_string_column"];
?>
期待db查询的输出以创建新数组
//cl = '[{"ifeid":1,"ans":"Yes","type":"SkipTo","target":"2"},{"ifeid":2,"ans":"Yes","type":"SkipTo","target":"5"}]';
cl = '<? echo $cl;?>';
//我想从保存的'cl'数组开始并将新项目推送到其中。
skptoQarry = new Array();
// javascript函数循环(未显示)生成var并推送到新数组。
thisItem_eid = 1;
yes_no_is_this = 'No';
SkipToTartgetEID = 5;
var skptoQarry_temp = {
"ifeid" : thisItem_eid,
"ans" : yes_no_is_this,
"type" : "SkipTo",
"target" : SkipToTartgetEID
};
skptoQarry.push(skptoQarry_temp);
cl = JSON.stringify(skptoQarry); //for ajax post to php for saving
//这是通过ajax发布保存数据库
[{“ ifeid”:1,“ ans”:“是”,“类型”:“ SkipTo”,“ target”:“ 2”},{“ ifeid”:2,“ ans”:“是” ,“ type”:“ SkipTo”,“ target”:“ 5”}]
// ...但是当PHP回显它时,只有这样:cl =“ [,]” //我认为我保存错误或以错误的方式回显列数据。
///从mysql读取文本并在需要时附加。
cl = $.parseJSON(cl);
jQuery.each(cl, function (i) {
jQuery.each(this, function (key, value) {
if (key == "ifeid") {
$('div').append('if this id: '+value+'<br>');
} else if (key == "ans") {
$('div').append('is: '+value+'<br>');
} else if (key == "type") {
$('div').append('then: '+value+'<br>');
} else if (key == "target") {
$('div').append('this id: '+value+'<br><br>');
}
});
});
function saveit(){
saved_logic_dialog = JSON.stringify(skptoQarry);
var posturl = "myurl?event=save&saved_logic_dialog="+saved_logic_dialog;
jQuery.ajax({
traditional: true,
type: "POST",
url: posturl,
success: function(data) {
//messages and stuff
}
});
}
// php
$loadvfsql = "SELECT `saved_logic_dialog` FROM `questions` WHERE `id` = '{$id}' ORDER BY `questions`.`question_order` ASC";
$loadv_result=mysql_query($loadvfsql);
while($rows=mysql_fetch_array($loadv_result)){
$clc = $rows['current_logic_cont'];
$cl = $rows['saved_logic_dialog'];
//more stuff
}
答案 0 :(得分:0)
这将确保对对象数组进行正确编码-jQuery不会为您编码URL。
var posturl = "myurl?event=save&saved_logic_dialog=" + encodeURIComponent(saved_logic_dialog);
htmlspecialchars($cl)
正确地转义可能在HTML中具有特殊含义的符号。JSON.parse(cl)
从String转换为Array。