Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to display Search Results on the same URL


Go to End


18 Posts   11738 Views

Avatar
yitter

Community Member, 10 Posts

16 January 2010 at 2:29am

Hi,

I'm using the standart Search Form setup. By default it displays the results attached to the URL of the page the search was coming from, for example:

/about/Search?Search=test ...
/contact/Search?Search=test ...

I'm trying to direct all search results to display under the same url like:

/search/Search?Search=test ...

I was doing it using $form->setFormAction :

		$fields = new FieldSet( new TextField("Search", "Search", '') );
		$searchAction = new FieldSet( new FormAction('results', 'Search') );
		$form = new SearchForm($this, "MetaSearch", $fields,$searchAction);
                $form->setFormAction(Director::baseURL().'results/Search');

BUT it does not work with IE for some reason. Other browsers append the variable "&action_results=Search" but IE doesn't. It's really frustrating ... now I'm wondering if there is another, better, way to achieve this?

Thanks for any hint!

Avatar
yitter

Community Member, 10 Posts

31 January 2010 at 10:49am

hmm, is this such a strange request? How do you guys do this?

Avatar
Terry Apodaca

Community Member, 112 Posts

16 March 2010 at 4:34am

Edited: 16/03/2010 4:41am

I was wondering something similar.

I have a search form that works just fine...on the home page of the site. but it keeps the home page URL. What I want to do is send all searches from all forms to one page, my Listings page. how can i set the form action to always go to /listings/SearchForm/?

Avatar
losqualo

Community Member, 17 Posts

5 September 2010 at 3:16am

I'd also like to understand how to do this.

It makes no sense to have identical search results returned on a variety of different urls due simply to the page from which the search ws launched... Whether searching from the homepage or some inner page the results for the same search query appear to be the same.

Other than this, and the formatting of the search form itself, the simple search form works fine... would just like a little more control over it.

Rob

Avatar
Martijn

Community Member, 271 Posts

5 September 2010 at 5:13am

You might want to create a seperate SearchPage Class (or only a SearchPage_Controller) to display the searchresults.

On the Page class, where the searchform is, you place a redirect to the SearchPage (or controller) in the searchform action.
Example:

/**
	 * Site search form 
	 */ 
	function SearchForm() {
		$searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Zoeken';
		$fields = new FieldSet(
	      	new TextField("Search", "", $searchText)
	  	);
		$actions = new FieldSet(
	      	new FormAction('results', 'Search!')
	  	);
	  	return new SearchForm($this, "SearchForm", $fields, $actions); 
	}
	
	/**
	 * Process and render search results
	 */
	function results($data, $form){
	  	$data = array(
	     	'Results' => $form->getResults(),
	     	'Query' => $form->getSearchQuery(),
	      	'Title' => 'Search Results'
	  	);
		
		//get corect URLSegment from SeachPage
		$searchpage = DataObject::get_one("Search");
	  	//return to submitted message
		Director::redirect(Director::baseURL(). $searchpage->Link()."/SearchForm/?Search=".$form->getSearchQuery()."&action_results=Search");
	}

You could create a seperate:
class SearchPage_Controller extends ContentController{
//formcode allowed actions etc
}

and add this to you _config.php

Director::addRules(10, array(
	'searchresults//$Action/$ID/$Batch' => 'SearchPage_Controller',
));

You can create a template in Layout calles Searchpage to display the searchresults.
In this way you have pageless controller to display the search resuls.

Hope this gets you started!

Avatar
losqualo

Community Member, 17 Posts

6 September 2010 at 8:14pm

Hi Martijn,

Thanks for the pointers...

I get an error though when I submit a search query:

[Notice] Undefined index: Search
GET /home/SearchForm?Search=fonts&action_results=Go%21

Line 100 in /Users/rob/Sites/experimental/wswss/may2010/public_html/wswsstripe/sapphire/search/SearchForm.php

Itself due to a failure in Page.php

The stack indicates the point of failure being this line within the results function (middle of setting up the $data array):

'Results' => $form->getResults(),

Why would that be happening?

Cheers

Rob

Avatar
Martijn

Community Member, 271 Posts

6 September 2010 at 8:33pm

What does :

Debug::show($data);
exit();

right before $data = array( //etc..)
outputs?

Avatar
losqualo

Community Member, 17 Posts

6 September 2010 at 9:15pm

I get the following, where 'fonts' was the query entered in the form.

Debug (Page_Controller->results() in line 61 of Page.php)

url =
/wswsstripe/home/SearchForm
Search =
fonts
action_results =
Go!

Go to Top