2016-09-07 97 views
1

我忙着學習ajax。我有一個預訂頁面和一個結帳頁面。發送PHP鏈接數據從ajax調用不同頁面onclick

我想做什麼

用戶類型承租人名稱在搜索框中,相匹配的名字被從數據庫中獲取和動態顯示。

像這樣:

reservations.php

enter image description here

當用戶點擊高亮顯示的鏈接,我想預約ID來獲得傳遞到結賬頁面。 THUS,我正在尋找一種方法將保留ID傳遞到另一個/不同的頁面,我可以使用它來在結帳頁面上進行數據處理。

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

在執行方面的任何幫助或建議,不勝感激!

+1

這將是非常相似,你已經與區別有,你需要趕上'click'事件,您現在已經在捕獲'keyUp'事件。確切的問題是什麼? – jeroen

+0

@ jeroen我需要1)從搜索框的名稱匹配中獲取res_id,根據名稱用戶點擊2)傳遞並填充到結帳屏幕表單字段...希望讓sence – Marilee

+0

爲什麼我有近距離投票在這個問題上,我確實付出了很多努力,試圖在符合準則的同時提供儘可能多的細節。 – Marilee

回答

5

只需使用鏈接和查詢。 起初呼應的鏈接,而不是一個div:

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

而且在頁面上簡單地做:

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

工作就像一個魅力,非常感謝你!最後一個問題,如果你不介意....我不需要'res_id'與名稱..thus關聯,我需要改變我的SQL查詢'$關鍵字= $ _GET ['關鍵字']; $ keyword =「%$ keyword%」; $查詢=「選擇renter_name,從保留哪裏renter_name喜歡?」'當我添加'查詢=「選擇res_id,renter_name,從保留哪裏renter_name喜歡?「;我得到的錯誤'綁定變量的數量不匹配準備語句中的字段數量'任何想法如何我可以解決這個問題? – Marilee

+0

@Marilee:你沒有錯誤$查詢(你在你的評論中寫了query =)? –

+0

對不起,它應該已經閱讀,'$ query',現在在它上面工作,認爲我在正確的路徑手指交叉 – Marilee