Reputation: 11
This is my php code:
<?php
require('connection.php');
$query="select title,content from blogs";
echo '<html><head>';
echo '<link rel="stylesheet" href="blog.css" />';
echo '<script type="text/javascript" src="blog.js"></script></head><body>';
$i=0;
if($result=$mysqli->query($query))
{
while($news=$result->fetch_row())
{
echo "<br /><br /><strong>". $news[0]."</strong><br /><br />";
if(strlen($news[1])>60)
{
$d=0;
$content=explode(" ",$news[1]);
foreach($content as $c)
{
if($d<=60)
{
echo $c." ";
$d++;
}
else
{
if($d==61)
{
echo "<div id=a$i style='visibility:hidden'>";
echo "<a href='#' onclick='toggle(a".$i.")' style='visibility:visible'>Show/Hide</a>";
$i++;
$d++;
}
echo $c." ";
$d++;
if($d==count($content)+1)
{
echo "</div>";
}
}
}
}
else
echo $news[1]."<br /><br />";
}
$result->close();
echo "</body></html>";
}
?>
This is my JavaScript code:
function toggle(id)
{
//document.getElementById('mcontent').value=x++;
//if(x%2==0)
document.getElementById(id).style.visibility="visible";
//else
//document.getElementById('mcontent').style.visibility="hidden";
};
The show hide button is not working.My strategy is to limit text which contains more than 60 words with the show hide button.Once the user clicks the button the hidden div becomes visible and that contains the extra text(namely the text after 60 word count).Div element should always start with a letter so I have append letter 'a' before that.Please help me fix this.
Note: Don't need Jquery solutions or suggestion.I need only to debug the above code.
Upvotes: 0
Views: 841
Reputation: 150020
The onclick
attribute on this line:
echo "<a href='#' onclick='toggle(a".$i.")' style='visibility:visible'>Show/Hide</a>";
...needs to have double-quotes around the parameter to toggle()
so that the browser receives it as onclick='toggle("a1")'
:
echo "<a href='#' onclick='toggle(\"a".$i."\")' style='visibility:visible'>Show/Hide</a>";
Upvotes: 2
Reputation: 1731
The parameter in toggle(a".$i.")
is a string. You should add string delimiters.
And also, you should add quotes to your attributes in yout HTML tags:
echo '<div id="' . a$i . '" style="visibility:hidden">';
Upvotes: 1
Reputation: 382102
You're missing quotes in your call to toggle
.
This would call toggle(a0)
instead of toggle("a0")
.
Try this :
echo "<a href='#' onclick=\"toggle('a".$i."')\" style='visibility:visible'>Show/Hide</a>";
As I always find painful to deal with this level of imbricated quotes, I prefer to avoid inlined javascript in PHP. You can defer the addition of handlers even without jQuery, using document.getElementById('...').onclick=...
.
Upvotes: 5