Marilee Marilee - 3 months ago 11
jQuery Question

Send PHP Link data from ajax call to different page onclick

Im busy learning ajax. I have a reservation page and a checkout page.

What I want to do

User types the renter name into a search box, matching names gets fetched from db and displayed dynamically.

Like so:

reservations.php

enter image description here

When user clicks the highlighted link, I would like the reservation Id to get passed to the checkout page. THUS, Im looking for a way to pass the reservation ID to another/different page, which I can use for data processing on the checkout page.

checkout.php

enter image description here

CODE

SEARCH / JQUERY

$(document).ready(function () {
$("#searchbox").on('keyup',function () {
var key = $(this).val();

$.ajax({
url:'fetch.php',
type:'GET',
data:'keyword='+key,
beforeSend:function () {
$("#results").slideUp('fast');
},
success:function (data) {
$("#results").html(data);
$("#results").slideDown('fast');
}
});
});
});
</script>
<div id="main">
<div id="header"><h1>Find Names</h1></div>
<div id="content">
<input type="search" name="keyword" placeholder="Search Names" id="searchbox">
<div id="results"></div>
</div>
</div>


FETCH.PHP

if($_GET['keyword'] && !empty($_GET['keyword']))
{
$conn = mysqli_connect('localhost','root','','*****'); //Connection to my database
$keyword = $_GET['keyword'];
$keyword="%$keyword%";
$query = "select renter_name from reservations where renter_name like ?";
$statement = $conn->prepare($query);
$statement->bind_param('s',$keyword);
$statement->execute();
$statement->store_result();
if($statement->num_rows() == 0) // so if we have 0 records acc. to keyword display no records found
{
echo '<div id="item">Ah snap...! No results found :/</div>';
$statement->close();
$conn->close();

}
else {
$statement->bind_result($name);
while ($statement->fetch()) //outputs the records
{
echo "<div id='item'>$name</div>";
};
$statement->close();
$conn->close();
};
};


DB

enter image description here

Any help or advice in terms of implementation greatly appreciated!

Answer

Simply use links and queries. At first echo a link instead of a div:

echo "<a href='yourpage.php?id=$name'>$name</a>";

And on your page simply do:

<?php
echo $_GET[" id"];
?>