Im trying to encode some html within json from PHP. The function requests data from a PHP file, and then will append it into the html page. The function works fine until I have < or > within the string I am trying to pass. This is my PHP code:
$append = "<b>Edit</b>";
for($i=0;$i<count($atarray);$i++)
{
$append .= $atarray[0]['atname'];
}
$json = json_encode(array('ok' => '0', 'apso' => $append));
echo $json;
This works fine, if instead of "<b>edit</b>" I use "Edit", is there a way to get it to work including the HTML tags?
The error that comes up in console is Uncaught SyntaxError: Unexpected token < jquery-1.7.js:2 whenever I run the function.
JavaScript
I’m using the jQuery form plugin -> http://archive.plugins.jquery.com/project/form
It’s sending the variables as POST data
but this is my function
$(function(){
$("#change").submit(function(e){
e.preventDefault();
$(this).ajaxSubmit({
beforeSubmit:function(before){
$('#loadingsignup').show();
$("#resultsignup").empty().hide();
},
success:function(retour){
$('#loadingsignup').hide();
res=jQuery.parseJSON(retour);
if(res.ok==1){
$('#resultsignup').append('<p class="good"><?php echo $lang['107']; ?></p>');
if(res.img!==undefined){
$('#eaim').attr('src','/images/artistsprofilepic/'+res.img);
}
}else if(res.apso!==undefined){
var ap=res.apso;
$('#apso').append(ap);
}else{
$('#resultsignup').append('<p class="pasgood">'+res.err+'</p>');
}
$("#resultsignup").hide().fadeIn(500);
setTimeout("$('#resultsignup').fadeOut();",5000);
}
});
});
});
Try getting rid of the call to
jQuery.parseJSON, and use the optiondataType: 'json'..ajaxSubmitusesjQuery.ajax, which tries to figure out the format of the response, and it may have determined that it was JSON, so it was already parsed. Parsing it again results in this error.