I am wondering how jquery/ajax can send a query to the server and then let the server (php) calculte the response, and append it to the webpage with jquery/ajax again?
Example : I have 3 products in database with the details for each product (name, description and image). I have a page with links to these 3 products and I want to display only the clicked product details with jquery/ajax :
html :
<nav>
<ul>
<li><a href="#prod1">prod1</a></li>
<li><a href="#prod2">prod2</a></li>
<li><a href="#prod3">prod3</a></li>
</ul>
</nav>
<div></div>
JS
$(document).ready(function() {
$('a').click(function(){
$('div').load("products.php");
});
});
this is what my products.php file looks like :
<?php
try
{$bdd = new PDO('mysql:host=localhost;dbname=ddb', 'root', '');}
catch (Exception $e)
{die('Error : ' . $e->getMessage());}
$reponse = $bdd->query('SELECT * FROM products ORDER BY id DESC') or die(print_r($bdd->errorInfo()));
?>
<div class="prod_details">
while ($data = $reponse->fetch()){ ?>
<h1><?php echo $products['name']; ?></h1>
<p><?php echo $products['description']; ?></p>
<img src="<?php echo $products['image_path']; ?>">
<?php }?>
</div>
This displays all the products, how can I get only the clicked product to be displayed?