meesh2175 Posted November 8, 2012 Share Posted November 8, 2012 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??? Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 I do not believe php variables work with javascript. You're also not closing your div tag. Quote Link to comment Share on other sites More sharing options...
meesh2175 Posted November 8, 2012 Author Share Posted November 8, 2012 I do not believe php variables work with javascript. You're also not closing your div tag. Any idea how I would incorporate javascript? I fixed the div tag, thanks! Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 Tested on my site and variables do work. Javascript is not my strong point; perhaps someone with more experience can help you Quote Link to comment Share on other sites More sharing options...
meesh2175 Posted November 8, 2012 Author Share Posted November 8, 2012 (edited) Tested on my site and variables do work. Javascript is not my strong point; perhaps someone with more experience can help you You tested my code above on your site and the buttons work??? If so, can I see? Edited November 8, 2012 by meesh2175 Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 8, 2012 Share Posted November 8, 2012 The php variables worked in javascript. The buttons however did not. Sorry for the confusion Quote Link to comment Share on other sites More sharing options...
meesh2175 Posted November 8, 2012 Author Share Posted November 8, 2012 The php variables worked in javascript. The buttons however did not. Sorry for the confusion Ahh gotcha! Thanks... I will search Google (my bestfriend btw lol) and see what I can find. Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 8, 2012 Share Posted November 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
meesh2175 Posted November 8, 2012 Author Share Posted November 8, 2012 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.