Jump to:

1970 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » How to display Search Results on the same URL

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

Page: 1 2 3
Go to End
Author Topic: 5291 Views
  • yitter
    Avatar
    Community Member
    10 Posts

    How to display Search Results on the same URL Link to this post

    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!

  • yitter
    Avatar
    Community Member
    10 Posts

    Re: How to display Search Results on the same URL Link to this post

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

  • Terry Apodaca
    Avatar
    Community Member
    109 Posts

    Re: How to display Search Results on the same URL Link to this post

    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/?

  • losqualo
    Avatar
    Community Member
    17 Posts

    Re: How to display Search Results on the same URL Link to this post

    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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: How to display Search Results on the same URL Link to this post

    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!

  • losqualo
    Avatar
    Community Member
    17 Posts

    Re: How to display Search Results on the same URL Link to this post

    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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: How to display Search Results on the same URL Link to this post

    What does :

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

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

  • losqualo
    Avatar
    Community Member
    17 Posts

    Re: How to display Search Results on the same URL Link to this post

    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!

    5291 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.