2

Let's imagine we have simple data and want to make pagination of it. It's not hard to do, simple _GET var with page number others doctrine with offset will allow us to do it in easy way, BUT How should it look like in search page? Let me explain.

For example we have simple route with /search url. Where we have form for our search. When use input string we user POST method on same page and will get result. Simple enough but if we add pagination here it become a problem with storing "inputed string".

If we store in session on search query it will be solution BUT... it's not. Why? User input search string - get result with pagination (here search string already in session) after that leave the page (or close browser, or left to another page). When he will return data from session will show him 'result of old query'...

So question is, what is the best practice for such situation? I want simple search query + pagination of it but if user left page - clear result.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
user1954544
  • 1,619
  • 5
  • 26
  • 53
  • 2
    Why are you using `POST` and not `GET` for a search form? Maybe I have misunderstood.... – Flukey Dec 14 '13 at 16:57
  • 1
    Unfortunately I have such specification. So I cannot use _GET var for search string. :( – user1954544 Dec 14 '13 at 17:55
  • Only not initially or overall? So that even the pageination links cannot contain the search query? – Markus Kottländer Dec 15 '13 at 08:56
  • We can add to $_GET search string but in this case we should have specific method that will escape this search string and redirect to our main search page, where filter will be applied. And it's really bad, very bad, practice. – user1954544 Dec 28 '13 at 03:50

1 Answers1

1

Using POST instead of GET for search query is kinda unusual and not really safe. Since search query operations are read-only you should use GET to access/get the data. POST is used for updating or creating resources.

And how you will go back/forward in the pagination (using browser's buttons)? You always will be getting an alert box. AND you cannot share/bookmark the search query url.

BTW to answer your question, sessions and hidden input fields would be the way to go. You also can use a combination of get and post

When should I use GET or POST method? What's the difference between them?

Community
  • 1
  • 1
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601