Click Button to Display/Hide DIV in PHP

General discussions related to php

Moderators: macek, egami, gesf

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

Click Button to Display/Hide DIV in PHP

Postby meesh2175 » 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??? :)

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

Re: Click Button to Display/Hide DIV in PHP

Postby seandisanti » 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 : http://www.w3schools.com/jquery/tryit.a ... ery_hide_p

line number 8 reads:

Code: Select all

    $("p").hide();

change it to

Code: Select all

    $("p").toggle();


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


Return to “PHP General”

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest

cron