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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度