How to make search results appear on another page?
I have a big search on my home-page and when the user types in the text fields and clicks submit I want the results from my database to appear on another site in this case 'searchresults.php.' In this case 'really.html' is my homepage.
Here is my code:
What am I doing wrong?
Really.html
<center>
<form action="searchresults.php" method="post">
<input type="text" size="35" value="Job Title e.g. Assistant Manager"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb"
onfocus="if(this.value == 'Job Title e.g. Assistant Manager'){this.value =
'';this.style.color='#000'}" />
<input type="text" size="35" value="Location e.g. Manchester"
style="background- color:white; border:
solid 1px #6E6E6E; height: 30px; font-size:18px;
vertical-align:9px;color:#bbb开发者_开发知识库"
onfocus="if(this.value == 'Location e.g. Manchester'){this.value =
'';this.style.color='#000'}" />
<input type="image" src="but.tiff" alt="Submit" width="60">
</form>
Searchresults.php
<html>
<body>
<?php
if(strlen(trim($_POST['search'])) > 0) {
//all of your php code for the search
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '$search' AND
location LIKE '$searchterm'";
$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) {
echo "<br>$row[0]<br>";
echo "$row[1]<br>";
echo "$row[2]<br>";
}
}
}
?>
</body>
</html>
Thanks!
You should add attr name to your input. For example,
<input type="text" name="search" ... />
<input type="text" name="searchterm" ... />
Also don't forget about escaping input data using mysqL_escape_string function
The query is probably returning 0 results. Instead of
if ($result) {
Try
if (mysql_num_rows($result) >= 1) {
And in your query try...
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'";
This will be less strict and return a better result set.
精彩评论