dti3914 2016-09-07 14:55
浏览 67
已采纳

将来自ajax调用的PHP链接数据发送到不同的页面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!

  • 写回答

1条回答 默认 最新

  • duandi5328 2016-09-07 15:00
    关注

    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"];
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?