Insert cakephp POST params into URL

2019-02-16 02:23发布

问题:

I have this form below which contains two checkboxes to sort some products:

<form id="FiltreExtraForm" action="" method="post" name="FiltreExtraForm">
    <input id="ProductsDeliveryPrice" type="checkbox" value="1" name="data[Products][delivery_price]"/>
    <input id="ProductsPicture" type="checkbox" value="1" name="data[Products][picture]"/>
</form>

After POST I do the filtering but I also want to add received parameters to URL E.g: /products/index/delivery_price:1/picture:0 . Is this possible. How can I do that?

Note: I don't want to use GET to send form info.

回答1:

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

The two main 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


回答2:

In the action to which you post, you could simply prepare the GET url and then redirect to this url. The action for that url then does the filtering.



回答3:

If I understand you correctly (and I'm not sure that I do), you can pass additional variables on the query string of the form's action quite easily. Conventionally, that might look like this:

<form id="FiltreExtraForm" action="/products/index?delivery_price=1&picture=0" method="post" name="FiltreExtraForm">

Using Cake, you should be able to do the same without the traditional query string if you'd rather (though the traditional method above will also work):

<form id="FiltreExtraForm" action="/products/index/delivery_price:1/picture:0" method="post" name="FiltreExtraForm"> 

I would recommend looking at the form helper or at least constructing the action URI using helpers, but this should get you what you're after.