Make search results show up on a different page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andy1212
Forum Commoner
Posts: 26
Joined: Sat Jan 22, 2011 2:30 pm

Make search results show up on a different page?

Post by andy1212 »

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:

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 />';
		}
	}
}

?>

search.php:

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.

?>

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Make search results show up on a different page?

Post by Celauran »

Point the form's action to your search page and process the search there.
andy1212
Forum Commoner
Posts: 26
Joined: Sat Jan 22, 2011 2:30 pm

Re: Make search results show up on a different page?

Post by andy1212 »

I tried that and it didn't work, it just echoed please enter a search term
andy1212
Forum Commoner
Posts: 26
Joined: Sat Jan 22, 2011 2:30 pm

Re: Make search results show up on a different page?

Post by andy1212 »

I tried that and it didn't work, it just echoed please enter a search term, I had the search form on the index.php page and the php code on the search.php page. I don't think I included the fun.inc.php on the search.php page though so ill try that and reply here with my results. Thanks for your help.
andy1212
Forum Commoner
Posts: 26
Joined: Sat Jan 22, 2011 2:30 pm

Re: Make search results show up on a different page?

Post by andy1212 »

Ok so including the func.inc.php in the code on the search.php page worked! lol I solved my own problem once again on a forum :)
Post Reply