I really need help.
My problem now is i want to display the books details, which the data is from mysql.
I tell step-by-step.
-
i have table books in mysql
-
from the table, i called the title and price using php, which display like this :
Title : One
Price : 20$
Title : Two
Price : 35$
my php code is :
<?php
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="";
@mysql_connect("$db_host","$db_username","$db_pass") or die("Could not connect to database.");
@mysql_select_db("$db_name") or die("No database");
$q=mysql_query("SELECT * FROM books ");
while($result = mysql_fetch_array( $q ))
{
echo "<b>Title:</b> ".$result['title'] . " <br> ";
echo "<b>Price:</b> RM".$result['price_myr'] ."<br>";
echo "<br>";
}
?>
- Here the problem start. I want to display the book details ( author, publisher, descrp, etc…) when user click on the list above ( step 2) . So can anyone give me the idea on how can i do that ? How to make the book list available for click and then it display the details.
example :
User click on listview ; Title:One ,
the details about the book popup or load.
I guess i need to use Ajax, but i dont know how.
Hope members here can help. Much thanks !
You should wrap each book in
<div>tags, and create anonclickfunction for each one.Whether it has the information pre-loaded or fetches the information on-demand is up to you. If you have a ton of books listed on the page, pre-loading the information might cause performance issues, so it may be better to load the book information on-demand, via AJAX.
Here is what your code might look like, using AJAX:
And your JavaScript function:
getBookInfo.php:
ALSO
One other side-note is that you should avoid the use of the
or die()syntax in a PHP script that is generating your HTML. See this link for some reasons why.