简体   繁体   中英

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

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

Any help or advice in terms of implementation greatly appreciated!

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"];
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM