Jquery in PHP echo 
<p>This is my php with jquery code:</p>

<pre><code>&lt;?php
require('connection.php');
$query="select title,content from blogs";
echo '&lt;html&gt;&lt;head&gt;';
echo '&lt;link rel="stylesheet" href="blog.css" /&gt;';
echo '&lt;script src="jquery.js"&gt;&lt;/script&gt;';
//echo '&lt;script src="blogs.js"&gt;&lt;/script&gt;';
echo "&lt;/head&gt;&lt;body&gt;";
$i=0;
if($result=$mysqli-&gt;query($query))
{
while($news=$result-&gt;fetch_row())
{
echo "&lt;br/&gt;&lt;br /&gt;"."&lt;strong &gt;&lt;h2 onclick='$(document).ready(function() {
    $(this).click(function(){ $(\"#a".$i."\").toggle(\"slow\");});});'&gt;". $news[0]."&lt;/h2&gt;&lt;/strong&gt;"."&lt;br/&gt;&lt;br /&gt;";
echo "&lt;div id='a".$i."'&gt;".$news[1]."&lt;/div&gt;";
$i++;
}
$result-&gt;close();
echo "&lt;/body&gt;&lt;/html&gt;";
}
?&gt;
</code></pre>

<p>This works but not stable. When I click the first heading it toggles for 2 to 3 times like an animation. When I click the second heading the first and second headings content toggles.This is the function call in the source code of the output php.</p>

<pre><code>&lt;strong &gt;&lt;h2 onclick='$(document).ready(function() {
    $(this).click(function(){ $("#a0").toggle("slow");});});'&gt;HELLO WORLD&lt;/h2&gt;&lt;/strong&gt;
</code></pre>

<p>Please let me know why it happens like this?I am new to jquery so I use it inline.I know it is a bad practice to do so and eliminates the biggest advantage of using jquery.</p>
