2015-04-15 45 views
0

我想構建一個頁面,顯示在html表格中搜索到的數據。我在頁面上設置了搜索字段和按鈕,並在當前顯示了所有數據的小型表格中。我需要它根據搜索字段中的詞主動更改表中顯示的數據。如何根據搜索結果動態更改表格中的數據?

編輯:

我怎麼能動態地根據在搜索字段中的文本改變表中的數據?

<?php 
    $servername = "localhost"; 
    $username = "username"; 
    $password = "password"; 
    $dbname = "database name"; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 

    $sql = "SELECT clientname, clientsurname, address1, postcode, dob FROM clients"; 
    $result = $conn->query($sql); 
    if ($result->num_rows > 0) { 
     echo "<table><tr><th>Name</th><th>Surname</th><th>Address</th><th>Postcode</th><th>Date of Birth</th></tr>"; 
     // output data of each row 
     while($row = $result->fetch_assoc()) { 
      echo "<tr><td>".$row["clientname"]."</td><td>".$row["clientsurname"]."</td><td>".$row["address1"]."</td><td>".$row["postcode"]."</td><td>".$row["dob"]."</td></tr>"; 
     } 
     echo "</table>"; 
    } else { 
     echo "There are 0 clients in the system matching your search criteria"; 
    } 
    $conn->close(); 
    ?> 
+0

你有queston? – Naruto

+0

抱歉,我忘了添加最後一行,現在編輯帖子並更新。不便之處 – DGTLSS

+0

看看ajax和jquery .. – Naruto

回答

0

@eselskas說了什麼+如果您想要通過搜索欄中的給定值進行搜索,則需要更改查詢。

$sql = "SELECT clientname, clientsurname, address1, postcode, dob FROM `clients` WHERE `your_field` LIKE '%$_POST[search_var]%'"; 

或更好:

if($_POST['change_var'] != ""){ 
    $sql = "SELECT clientname, clientsurname, address1, postcode, dob FROM `clients` WHERE `your_field` LIKE '%$_POST[search_var]%'"; 
}else{ 
    $sql = "SELECT clientname, clientsurname, address1, postcode, dob FROM `clients`"; 
} 
+0

謝謝你的幫助,我會試試看 – DGTLSS

0

最好的解決辦法是創建一個Ajax請求到後端,搜索數據庫並返回其將被追加到HTML的JSON響應。

但是,這會要求重新分解代碼並拆分前端和後端邏輯。

相關問題