2017-04-04 62 views
-1

的search.phpPHP搜索,但顯示結果在接下來的頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<!DOCTYPE html> 
<html> 
<head> 
<style> 
ul { 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    overflow: hidden; 
    background-color: #333; 
} 

li { 
    float: left; 
} 

li a { 
    display: block; 
    color: white; 
    text-align: center; 
    padding: 14px 16px; 
    text-decoration: none; 
} 

li a:hover { 
    background-color: #111; 
} 
</style> 
</head> 

<body> 
<?php 
    $query = $_GET['query']; 
    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

     $query = mysql_real_escape_string($query); 
     // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT * FROM register 
      WHERE (`Username` LIKE '%".$query."%') OR (`Firstname` LIKE '%".$query."%')") or die(mysql_error()); 

     // * means that it selects all fields, you can also write: `id`, `title`, `text` 
     // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query is Hello 
     // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

     if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

      while($results = mysql_fetch_array($raw_results)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       echo "<p><h3>".$results['Username']."</h3>".$results['Contactnumber']."</p>"; 
       // posts results gotten from database(title and text) you can also show id ($results['id']) 
      } 

     } 
     else{ // if there is no matching rows do following 
      echo "No results"; 
     } 

    } 
    else{ // if query length is less than minimum 
     echo "Minimum length is ".$min_length; 
    } 
?> 
</body> 

的index.php

<!DOCTYPE html> 
<html> 
<head> 
<style> 
ul { 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    overflow: hidden; 
    background-color: #333; 
} 

li { 
    float: left; 
} 

li a { 
    display: block; 
    color: white; 
    text-align: center; 
    padding: 14px 16px; 
    text-decoration: none; 
} 

li a:hover { 
    background-color: #111; 
} 
</style> 
</head> 
<body> 
<body bgcolor="#919191"> 

<ul> 
    <li><a class="active" href="#home">Dashboard</a></li> 
    <li><a href="#news">Add Package</a></li> 
    <li><a href="#contact">View Customer</a></li> 
    <li><a href="#about">View Order</a></li> 
</ul> 

<form action="search.php" method="GET"> 
     <input type="text" name="query" /> 
     <input type="submit" value="Search" /> 
    </form> 



</body> 
</html> 

是在同一頁面上展示可能的結果,而不是顯示在下一頁? 因爲我的結果顯示爲空白,我想在我的頁面上顯示,任何人都知道在哪裏解決問題?我試圖加入,而不是幫助。

+2

合併的search.php代碼就能解決問題 –

+2

避免使用MySQL的功能(),因爲這些現在在PHP棄用7 –

回答

0

你在找什麼是AJAX(異步JavaScript和XML)。它允許您在當前頁面實際加載時執行後門請求。

這就像你跟別人說話然後很快就會向另一個人竊竊私語,第一個人不知道這件事。在這個例子中,你是瀏覽器,第一個人是你(客戶),第三個是網絡服務器。

因此,通過AJAX可以將數據發送到一個php文件並獲得答案。請自己看看。

1

那麼,很容易,有沒有辦法在PHP中...你需要使用AJAX。我的朋友做了一個很大的AJAX庫,您可以在Github上找到:

https://github.com/PDKnight/XXHR

那裏你可以學到最基礎,讓你尋找工作,只要你想!

OR

你可以用網站刷新,這看起來並不好,但無論做...

  1. 變更表單動作爲「」(是的,讓它空白)
  2. 添加你的PHP的index.php
  3. 只能調用你的PHP如果提交被稱爲:

    如果($ _ POST [ 「提交」]){// 你的PHP有 }

所以它可能工作像這樣..用戶進入該網站,沒有任何反應,然後輸入值形成,點擊提交,這會導致網站刷新,然後查看結果,因爲php知道他按下了提交。易鬆動的檸檬吱吱...

0

這是非常簡單的,只需從窗體標籤中刪除search.php。在index.php中

<?php 
    $query = $_GET['query']; 
    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

     $query = mysql_real_escape_string($query); 
     // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT * FROM register 
      WHERE (`Username` LIKE '%".$query."%') OR (`Firstname` LIKE '%".$query."%')") or die(mysql_error()); 

     // * means that it selects all fields, you can also write: `id`, `title`, `text` 
     // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query is Hello 
     // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

     if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

      while($results = mysql_fetch_array($raw_results)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       echo "<p><h3>".$results['Username']."</h3>".$results['Contactnumber']."</p>"; 
       // posts results gotten from database(title and text) you can also show id ($results['id']) 
      } 

     } 
     else{ // if there is no matching rows do following 
      echo "No results"; 
     } 

    } 
    else{ // if query length is less than minimum 
     echo "Minimum length is ".$min_length; 
    } 
?> 
    <!DOCTYPE html> 
    <html> 
    <head> 
    <style> 
    ul { 
     list-style-type: none; 
     margin: 0; 
     padding: 0; 
     overflow: hidden; 
     background-color: #333; 
    } 

    li { 
     float: left; 
    } 

    li a { 
     display: block; 
     color: white; 
     text-align: center; 
     padding: 14px 16px; 
     text-decoration: none; 
    } 

    li a:hover { 
     background-color: #111; 
    } 
    </style> 
    </head> 
    <body> 
    <body bgcolor="#919191"> 

    <ul> 
     <li><a class="active" href="#home">Dashboard</a></li> 
     <li><a href="#news">Add Package</a></li> 
     <li><a href="#contact">View Customer</a></li> 
     <li><a href="#about">View Order</a></li> 
    </ul> 

    <form action="" method="GET"> 
      <input type="text" name="query" /> 
      <input type="submit" value="Search" /> 
     </form> 



    </body> 
    </html> 
+0

說明:未定義指數:查詢中d:\ XAMPP \ htdocs中\第2行的adminsearch1.php – thenewsboy