Home >Backend Development >PHP Tutorial > php中兑现搜索框
php中实现搜索框
我做出来的网页效果是这样的:
怎样才能更具搜索框中的关键字在第二个页面显示搜索结果呢?
我的代码如下:
这是html的代码:
<br /> <form method="post" action="result.php" class="search"><br /> <input type="text" size="30" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" /><br /> <input type="submit" value="搜索" id="send" /><br /> <div class="suggestionsBox" id="suggestions" style="display: none;"><br /> <img src="../images/upArrow.png" style="max-width:90%" alt="upArrow" /><br /> <div class="suggestionList" id="autoSuggestionsList"><br /> <br /> </div><br /> </form><br /> <scripit><br /> function lookup(inputString) {<br /> if(inputString.length == 0) {<br /> // Hide the suggestion box.<br /> $('#suggestions').hide();<br /> } else {<br /> $.post("rpc.php", {queryString: ""+inputString+""}, function(data){<br /> if(data.length >0) {<br /> $('#suggestions').show();<br /> $('#autoSuggestionsList').html(data);<br /> }<br /> });<br /> }<br /> } // lookup<br /> <br /> function fill(thisValue) {<br /> $('#inputString').val(thisValue);<br /> setTimeout("$('#suggestions').hide();", 200);<br /> }<br /> </script><br />
<br> <?php <br /> require_once 'db_fns.php';<br> header("Content-type: text/html; charset=gb2312");<br> $db = db_connect();<br> if(!$db) {<br> // Show error if we cannot connect.<br> echo 'ERROR: Could not connect to the database.';<br> } else {<br> // Is there a posted query string?<br> if(isset($_POST['queryString'])) {<br> $queryString = $db->real_escape_string($_POST['queryString']);<br> <br> // Is the string length greater than 0?<br> <br> if(strlen($queryString) >0) {<br> // Run the query: We use LIKE '$queryString%'<br> // The percentage sign is a wild-card, in my example of countries it works like this...<br> // $queryString = 'Uni';<br> // Returned data = 'United States, United Kindom';<br> <br> // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.<br> // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10<br> <br> $query = $db->query("SELECT * FROM bbstopic WHERE title LIKE '%$queryString%' LIMIT 10");<br> if($query) {<br> // While there are results loop through them - fetching an Object (i like PHP5 btw!).<br> while ($result = $query ->fetch_object()) {<br> // Format the results, im using