<?
$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=新数组();
//javascript函数循环(未显示)生成变量并推送到新数组。
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 post保存在db中的内容
[{“ifeid”:1,“ans”:“yes”,“type”:“skipto”,“target”:“2”},{“ifeid”:2,“ans”:“yes”,“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
}
1条答案
按热度按时间hfsqlsce1#
这将确保对象数组的编码正确-jquery不会为您编码url。
保存到db时-检查是否正确转义值(因为它肯定包含引号);
将值回显到html时-使用
htmlspecialchars($cl)
正确地转义在html中可能具有特殊意义的符号。在javascript中使用值之前-使用
JSON.parse(cl)
从字符串到数组的转换。