1

How do I submit a form using the form helper and have the reply of that submission have a url with what was searched for?

I submit this code:

 <?php echo $form->create('Search', array('action' => 'results', 'type' => 'post')); ?>
    <?php

            $options = array
            (
                'size' => 45,
                'id' => 'search',
                'tabindex' => 1,
                'maxlength' => 250
            );

            echo $form->text('Search.query', $options);
        ?>

So when I submit the form with the words "Hello World", I want the resulting url to be:

 [domain]/searches/results/Hello+World

1 Answer 1

2

You will have to do a redirect to get this exact URL. Submitting a form using GET would result in /searches/results?SearchQuery=Hello+World. For my taste that would be perfectly adequate, but if you want a pretty URL, do this in your controller:

class SearchesController extends AppController {
    public function results($query = null) {
        if (!$query && $this->data) {
            $this->redirect(array('action' => 'searches', $this->data['Search']['query']));
        }

        // search
     }
}

Note that this requires one extra roundtrip to the server.

Sign up to request clarification or add additional context in comments.

4 Comments

i'm having trouble when i search for words that have a space...seems weird
What trouble in particular? Examples?
nevermind, having some caching issues and debug code messing things up
also added another if clause that stated if $query and $this->data exists, then they also need to be equal, or else redirect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.