【问题标题】:PHP search , but show result in next pagePHP搜索,但在下一页显示结果
【发布时间】:2017-08-29 09:15:22
【问题描述】:

搜索.php

<!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>

是否可能在同一页面上显示结果而不是在下一页上显示? 由于我的结果显示为空白,我想在我的页面上显示,有人知道在哪里解决问题吗?我试图添加,没有帮助。

【问题讨论】:

  • 在 index.php 中合并 search.php 代码即可解决问题
  • 避免使用 mysql function() 因为这些现在在 php 7 中已被弃用

标签: php html mysql database search


【解决方案1】:

嗯,很简单,在php中没有办法做到这一点......你需要使用AJAX。我的朋友做了一个很棒的 AJAX 库,你可以在 Github 上找到:

https://github.com/PDKnight/XXHR

在那里,您可以学习非常基础的知识并根据需要进行搜索!

你可以通过网站刷新来做到这一点,这看起来不太好,但是无论如何......

  1. 将表单操作更改为“”(是的,留空)
  2. 将你的 PHP 添加到 index.php
  3. 仅在调用提交时调用您的 php:

    如果($_POST[“提交”]){ //你的php在那里 }

所以它可能会这样工作..用户进入站点,没有任何反应,然后输入值到表单,点击提交,这导致站点刷新,然后查看结果,因为php知道他按下提交。简单的 peasy 柠檬榨汁……

【讨论】:

    【解决方案2】:

    您正在寻找的是 AJAX(异步 JavaScript 和 XML)。它允许您在当前页面实际加载时执行后门请求。

    这就像您与某人交谈,然后迅速对另一个人耳语,而第一个人不知道这一点。在此示例中,您是浏览器,第一个人是您作为个人(客户端),第三个人是网络服务器。

    因此,通过 AJAX 可以将数据发送到 php 文件并获得答案。请自行查找。

    【讨论】:

      【解决方案3】:

      这很简单,只需从表单标签中删除 search.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>
      

      【讨论】:

      • 注意:未定义索引:第 2 行 D:\xampp\htdocs\adminsearch1.php 中的查询
      猜你喜欢
      • 2019-10-03
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多