2013-03-22 91 views
0

我有一個小問題。我使用本教程基於數據庫值創建onclick表

http://www.codemashups.com/dynamic-search-filtering-using-jquery-and-php/

它讓我搜索我的數據庫,並在表中返回結果。 這是在網頁「搜索data.php」代碼的基礎

if ($conn) { 

    /* Fetch the users input from the database and put it into a 
    valuable $fetch for output to our table. */ 
    $fetch = mysql_query("SELECT * FROM clients 
         WHERE client_name LIKE '%{$param}%' OR client_email REGEXP '^$param' OR client_address LIKE '%{$param}%' OR client_postcode LIKE '%{$param}%' OR client_phone REGEXP '^$param'"); 

    /* 
     Retrieve results of the query to and build the table. 
     We are looping through the $fetch array and populating 
     the table rows based on the users input. 
    */ 
    while ($row = mysql_fetch_object($fetch)) { 
     $sResults .= '<tr id="'. $row->id . '" >'; 
     $sResults .= '<td>' . $row->client_name . '</td>'; 
     $sResults .= '<td>' . $row->client_email . '</td>'; 
     $sResults .= '<td>' . $row->client_address . '</td>'; 
     $sResults .= '<td>' . $row->client_postcode . '</td>'; 
     $sResults .= '<td>' . $row->client_phone . '</td> 
     </tr>'; 
    } 

} 

/* Free connection resources. */ 
mysql_close($conn); 

/* Toss back the results to populate the table. */ 
echo $sResults; 

這一切工作正常,並用我的表,我可以搜索並顯示我想你問的信息,太棒了!那你想要什麼?

那麼,我的表是一個商店的客戶端信息,而在以前沒有可搜索的表格中,您從列表中點擊您想要的客戶端,然後轉到其所有信息頁面(列表中的信息僅限於姓名和地址,因爲整個頁面有電話,郵政編碼等),並使用onclick。

因此,這裏是我的新搜索表的表:

  <table> 
       <tr> 
        <th class="ui-state-default">Name</th> 
        <th class="ui-state-default">Email</th> 
        <th class="ui-state-default">Address</th> 
        <th class="ui-state-default">Postcode</th> 
        <th class="ui-state-default">Phone</th> 
       </tr> 
      <tbody id="results" onclick="location='../clients.php?action=viewClient&amp;clientId=<?php echo ?>'" ></tbody> 
     </table> 

就像我說的它的偉大工程,所有的客戶信息來了,但我需要有「身份證」之間搞出在onclick線上的回聲(我想如果它顯示正確的ID不需要回聲)。

onclick="location='../clients.php?action=viewClient&amp;clientId=<?php echo ?> 

因此,誰能看到一個簡單的方法來獲取ID進入的onclick的信息,我試圖創建的「搜索data.php」頁面<tr>部分的點擊數,但它沒有工作要麼因爲它不會,要麼我做錯了,後來我在想。

反正任何幫助將是偉大的。

--------------------編輯------------------------- -

我發現我可以創建一個有效的鏈接。我可以創建這樣的結果的鏈接:

$sResults .= '<td><a href="../clients.php?action=viewClient&amp;clientId='. $row->id .'">'. $row->client_name .'</a></td>'; 

此創建一個工作環節,但是當我嘗試做了類似的事情的onclick它甚至不去任何地方,更不用說正確的地方,如如果它不喜歡這樣做,如果這有助於任何人進一步處理好的onclick問題。

+0

您是否創建了一個可以生成表格的php頁面? – 2013-03-22 16:30:52

+0

你應該簡單地使用真實的鏈接... – soju 2013-03-22 16:30:55

+0

我不使用真正的鏈接,我有一個名爲viewClient的頁面,它是基於id動態填充的,所以如果鏈接中的id是8,將顯示不同用戶的詳細信息如果它是1 – snookian 2013-03-22 16:35:19

回答

0

既然你已經使用jQuery刪除你的桌子上的onclick並添加以下的javascript:

基於教程,如果您需要更新您的搜索頁面的頭部部分這一點,你不該」你不得不更新你的任何其他代碼才能工作。

<head> 
    <title>jQuery Search Autocomplete</title> 

    <style type="text/css" title="currentStyle"> 
     @import "css/grid_sytles.css"; 
     @import "css/themes/smoothness/jquery-ui-1.8.4.custom.css"; 
    </style> 

    <!-- jQuery libs --> 
    <script type="text/javascript" src="js/jquery-1.6.1.min.js"></script> 
    <script type="text/javascript" src="js/jquery-ui-1.7.custom.min.js"></script> 

    <script type="text/javascript" src="js/jquery-search.js"></script> 

    <script type="text/javascript"> 
     (function($) { 
      $('td').click(function() { 
       var id = $(this).parent().attr('id'); 
       window.location = '../clients.php?action=viewClient&clientId=' + id; 
      } 
     }(jQuery)); 
    </script> 
</head> 
+0

我不知道Jquery太多了,但仍然不知道如何從「search-data.php」頁面獲取id到這個。 – snookian 2013-03-22 16:56:24

+0

第三行是處理檢索ID的方法。所以基本上,如果在表中點擊任何一個單元格,它會到達TR標籤的父級,然後查看該標識符上的id屬性並將ID屬性存儲到變量id中,然後將該id附加到字符串來生成窗口位置。 – 2013-03-22 16:58:33

+0

我更新了我的答案,希望這會有所幫助。 – 2013-03-22 17:01:41