2017-04-04 91 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

相关问题