views:

294

answers:

2

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!

+3  A: 

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.

nickf
Hmm, I was afraid of this HTTP request duplication. Well, I suppose it's the only solution left. Thanks!
Jasie
did you try the javascript solution?
nickf
Yeah, that worked. Thanks!
Jasie
A: 

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
deizel