Click Button to Display/Hide DIV in PHP

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Feb 16, 2012 1:03 pm

Thu Nov 08, 2012 7:29 am

Hello, I have created a simple HTML page which displays a series of questions. Each question has a "View Answer" and "Hide Answer" button. The answer is hidden in a DIV which displays and hides when the buttons are clicked.

I have now incorporated a search function using PHP which lets the user search for questions by keyword. It searches my database for any questions with that keyword and displays them on the page. I can't figure out how to make the buttons work to display the answer similar to my HTML page.

Here is what I have so far:

Code: Select all

while($row = mysql_fetch_array($result)){
$id = nl2br($row ['id']);
echo "<tr><td>Question: </span>". nl2br($row ['question']). "<br>";
echo "<input type='button' class='buttonQuestion' onClick='document.getElementById('q$id')style.display='block';' value='View Answer'>";
echo "<input type='button' class='buttonQuestion' onClick='document.getElementById('q$id')style.display='none';' value='Hide Answer'>";
echo "<div id='q$id' style='display:none;'>Answer: ".nl2br($row ['answer']). "</div";
Right now it displays the question and buttons... it hides the answer but my buttons don't work when you click on them. Any ideas??? :)

php-forum Fan User
php-forum Fan User
Posts: 974
Joined: Mon Oct 01, 2012 12:32 pm

Mon Nov 12, 2012 2:42 pm

JQuery is probably the easiest way to accomplish what you're looking for, check out this example from w3schools : ... ery_hide_p

line number 8 reads:

Code: Select all

change it to

Code: Select all

then hit submit code and the panel on the right will do as you wish.

Post Reply