Jump to content

Click Button To Display/hide Div In Php


meesh2175

Recommended Posts

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:

 

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??? smile.gif

Link to comment
https://forums.phpfreaks.com/topic/270460-click-button-to-displayhide-div-in-php/
Share on other sites

The terminology used in this thread is misleading.

 

PHP variables are NOT used in Javascript. PHP and Javascript are independent of one another and do not work together. There are two main ways where there is a connection between PHP and Javascript:

 

1. Using AJAX, Javascript can make requests to a PHP page. And the PHP page can return a result to the Javascript.

 

2. PHP can be used to write dynamic Javascript code to the page. But, once the page is complete and sent to the user the PHP code is done - there is no more execution.

 

What this thread seems to be about is item #2. So, talking about "PHP variables working in Javascript" is incorrect. The PHP variables can be used to write out Javascript just as PHP can be used to write out HTML. This problem is a Javascript problem. You simply need to take your working HTML page and then create PHP code to dynamically write out the content to match the same format.

 

Writing code with code can be confusing. So, if it is not working, then view the HTML source and see what is off and make changes till it is as it should be.

The terminology used in this thread is misleading.

 

PHP variables are NOT used in Javascript. PHP and Javascript are independent of one another and do not work together. There are two main ways where there is a connection between PHP and Javascript:

 

1. Using AJAX, Javascript can make requests to a PHP page. And the PHP page can return a result to the Javascript.

 

2. PHP can be used to write dynamic Javascript code to the page. But, once the page is complete and sent to the user the PHP code is done - there is no more execution.

 

What this thread seems to be about is item #2. So, talking about "PHP variables working in Javascript" is incorrect. The PHP variables can be used to write out Javascript just as PHP can be used to write out HTML. This problem is a Javascript problem. You simply need to take your working HTML page and then create PHP code to dynamically write out the content to match the same format.

 

Writing code with code can be confusing. So, if it is not working, then view the HTML source and see what is off and make changes till it is as it should be.

 

Brilliant! Thanks... I will try that. Thanks for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.