Correct way to escape a string in PHP when there are row['value'] in the string?
The code below is returned to an html page in an object using AJAX.
It doesn't seem to do what It does when I am not echoing it as PHP, which is to sort the DIVS as they are created based on the attribute 'data-sort'
I know that the function is correct, am I perhaps escaping it incorrectly?
echo "
newdiv = $(\'<div id=\'eventdiv\' class=\'shadow curved_sml gradient\' data-sort=\'".$row_display['TimeStamp']."\'>".$row_new['Title']." TIME: ".$row_display['TimeStamp']."开发者_Python百科</div>\');
div_id_after = Math.floor(parseFloat(newdiv.attr('data-sort')));
$('div[data-sort='+div_id_after+']').after(newdiv);
";
Regards, Taylor
Yes, you are escaping it incorrectly. What you have is:
- first level of quotes: double ones, in PHP,
- second level of quotes: single ones, in JavaScript,
but you have incorrect quotes in the third level - HTML passed as string in JS, which in turn is passed as string in PHP. Also you should not escape '
's, because you enclosed them in "
's.
Try the following:
echo "
newdiv = $('<div id=\"eventdiv\" class=\"shadow curved_sml gradient\" data-sort=\"" . $row_display['TimeStamp'] . "\">" . $row_new['Title'] . " TIME: " . $row_display['TimeStamp'] . "</div>');
div_id_after = Math.floor(parseFloat(newdiv.attr('data-sort')));
$('div[data-sort=\"'+div_id_after+'\"]').after(newdiv);
";
I think this should solve your problem. But I did not test it.
Yes, you are - in a double-quoted string, you don't escape single quotes. Remove all of those \'
s and replace them with just '
.
精彩评论