How to stop an infinite loop when Echoing Javascript from PHP page as an Ajax Response?
Basically, the code below will generate divs which are then captured using the jquery .ajax function as the data object and appended to the div with ID='textResp':
while($row = mysql_fetch_array($result_new))开发者_高级运维
{
/* Echo the result as Jquery */
echo "
<script>
jQuery(document).ready(function(){
$('#textResp').append(<div>".$row['Title']."</div>');
});
</script>";
}
This code works fine, but when I add a class to the div:
$('#textResp').append(<div class='class'>".$row['Title']."</div>');
The whole world blows up and it sends the ajax calls into an infinite loop.
Has anyone ever heard of this... or am I just writing my javascript wrong?
I know that I shouldn't be echoing javascript, but I really didn't want to have to explode the result of the string captured in the 'data' object to add the '<div class='class'>explode_result_1</div>'
I don't see how this would cause an infinite loop, but you have quote issues. First, you're missing one before the <div
. But more importantly, you'll need to escape some of those quotes. You have double quotes used for your php strings and then single quotes within that used for your javascript. The problem comes in when you put a class and try and use single quotes for that too, it ends the javascript string and just makes a mess. Try this:
"$('#textResp').append('<div class=\'class\'>".$row['Title']."</div>');"
$('#textResp').append('<div class="class">'.$row['Title']."</div>");
You have to use double quotes in HTML (I think that's a must), but you are also missing an opening single quote in your append parameter. Change to this:
$('#textResp').append('<div class=\"class\">'".$row['Title']."</div>");
精彩评论