CakePHP: Prevent GET form fields in URL
I've got a CakePHP search form that has 'type'=>'get'
. Basically, one of the e开发者_开发百科lements 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!
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.
Sounds like you are looking to do a Post/Redirect/Get.
Here are two examples of doing this in CakePHP:
- Searching on surname
- Searching on multiple fields
Advantages of redirecting a POST
to a GET
request are:
- Users don't get the "Do you want to resubmit?" dialog if they refresh
- The resulting page/query can be bookmarked
- 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'**));
精彩评论