Make search results show up on a different page?
Posted: Tue Apr 03, 2012 12:18 pm
Hey, I followed a tutorial a little while ago to create a search feature for my website. It works great and everything but I was wondering if there was a way of have the search bar on the index page and the results on the search page using the code I have for the search feature below. Right now the results are just displayed on the same page as the search form. When I go to most websites, someone searches something and the results are shown on a seperate page with the option to search again. How would I be able to do this too. Thanks for your time.
here's my code,
index.php:
search.php:
here's my code,
index.php:
Code: Select all
<?php include 'func.inc.php' ;?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search</title>
</head>
<body>
<h2>Search</h2>
<form action="" id="" method="post">
<p>
<input type="text" name="keywords" /> <input name="submit" type="submit" value="Search" />
</p>
</form>
<?php
// is there anyway of moving some of this code to the search.php page so the results display there?
if (isset($_POST['keywords'])) {
$suffix = '';
$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
$errors = array();
if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3) {
$errors[] = 'Your search term must be 3 or more characters';
} else if (search_results($keywords) === false) {
$errors[] = 'Your search for '.$keywords.' returned no results';
}
if (empty($errors)) {
$results = search_results($keywords);
$results_num = count($results);
$suffix = ($results_num != 1) ? 's' : '';
echo '<p>Your search for <strong>', $keywords, '</strong> returned <strong>', $results_num, '</strong> result', $suffix, '</p>';
foreach ($results as $result) {
echo '<p><strong>', $result['title'], '</strong> <br /> ', $result['description'], '... <br /> <a href="', $result['url'], '" target="_blank">', $result['url'], '</a></p>';
}
} else {
foreach ($errors as $error) {
echo $error, '<br />';
}
}
}
?>
Code: Select all
<?php
// Here is where the search results would show up with the search bar again giving the viewer the option to search again on the same page.
?>