Question

I've got a CakePHP search form that has 'type'=>'get'. Basically, one of the elements in the form is a submit button of type image. When the form is posted, in the URL I always get these x & y coordinates of the image submit button:

http://site.com/controller/action?x=22&y=36&query=hello

Is there any way I can prevent the coordinates from showing up in the URL? The reason is so that someone else could use the same URL to perform the same search, without that unintuitive stuff in the link.

Thanks!

Was it helpful?

Solution

You could use some javascript on the button:

document.getElementById('myImageButton').onclick = function() {
    this.form.submit();
    return false;
};

Alternatively, in your controller in the beforeFilter function, you could check for the presence of the unwanted variables, strip them out and redirect to the nice URL. This does mean there'll be 2 HTTP requests made though.

OTHER TIPS

Sounds like you are looking to do a Post/Redirect/Get.

Here are two examples of doing this in CakePHP:

Advantages of redirecting a POST to a GET request are:

  1. Users don't get the "Do you want to resubmit?" dialog if they refresh
  2. The resulting page/query can be bookmarked
  3. You can utilise CakePHP's built-in SEF routing, so instead of URLs with /search?q=contact you can get /search/contact

Instead of using submit helper function, use button function and set button type to submit.

echo $this->Form->**button**($this->Html->image('header_search_icon.png'), array(**'type'=>'submit'**));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top