Cannot parse array with HTML code using jQuery's $.parseJSON -
i trying use jquery's $.parsejson() convert variable json 1 of values in json string consists of html code. when remove html variable , replace 'test', variable can parsed using $.parsejson , sent php script using ajax objects can manipulated.
i trying figure out how pass html code variable , parse in order make object accessible php script. currently, questionshtml[index] variable breaking script.
i've tried using 'questionsvalues[index].replace("}{", "},{");' in order remove double quotes , backslashes script still breaks. can provide solution getting html code prepared parsed using $.parsejson() ?
javascript code
var questionstype = []; var questionshtml = []; var questionsquestion = []; //store questions, types, , html each fieldset 3 separate arrays for(var i=1;i<=formpreviewid;i++) { questionsquestion.push($("#formelement_"+i + " legend").text()); questionstype.push($("#formelement_"+i).attr("class")); questionshtml.push($("#formelement_"+i)[0].outerhtml); }; //format values each fieldset values format in mysql var questionsvalues = []; var index = 0; for(var i=1;i<=formpreviewid;i++) { questionsvalues.push('{"question":"'+questionsquestion[index]+'","type":"'+questionstype[index]+'","html":"'+questionshtml[index]+'"}'); index++; }; //format mysql values json format var questionsvaluesjson = '{"questions":['+questionsvalues+']}'; questionsvaluesjson = $.parsejson(questionsvaluesjson); //convert json var jsonarray = json.stringify(questionsvaluesjson); $.ajax({ type: 'post', url: '/project/templates/yoo_nano2/php/saveform.php', data: {'formpreviewhtml': formpreviewhtml, 'jsonarray': jsonarray}, beforesend:function(){ // append loading image $("#formpreview").append('<div style="z-index:3000;" id="divoverlaycontainer"><div id="loaderoverlay"><div class="center" id="loader"><img src="/project/templates/yoo_nano2/images/loader.gif"></div></div></div>'); }, success:function(data){ alert(data + "saved form successfully."); //$('#viewresults').empty(); $('#divoverlaycontainer').remove(); // successful request; data //$('#viewresults').html(data); }, error:function(){ // failed request; give feedback user alert("save unsuccessful. please try again."); $('#divoverlaycontainer').remove(); } }); }); });
Comments
Post a Comment