PHP搜索,但在下一页显示结果

时间:2017-04-04 09:03:17

标签: php html mysql database search

search.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>

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

3 个答案:

答案 0 :(得分:1)

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

https://github.com/PDKnight/XXHR

在那里,您可以学习基础知识并使搜索工作正常进行!

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

  1. 将表单操作更改为&#34;&#34; (是的,让它空白)
  2. 将您的PHP添加到index.php
  3. 只有在调用提交时才调用您的php:

    如果($ _ POST [&#34;提交&#34;]){ //你的PHP在那里 }

  4. 所以它可能会这样工作..用户进入网站,没有任何反应,然后输入值形成,点击提交,这会导致网站刷新,然后看到结果,因为PHP知道他按下了提交。易腻柠檬吱吱......

答案 1 :(得分:0)

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

这就像你和别人说话,然后很快向另一个人耳语,第一个人不知道这件事。在这个例子中,你是浏览器,第一个人就是你(客户),第三个是网络服务器。

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

答案 2 :(得分:0)

非常简单,只需从表单标记中删除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>