Welcome Guest, Not a member yet? Register   Sign In
Pagination in a POST search form/results
#1

[eluser]gh0st[/eluser]
Here's my situation.

1. A form where a user selects data to search upon.
2. A user presses 'go' and a MySQL search is performed.
3. The results are displayed, paginated, starting at page 1.

However, whenever I press on page 2 - the data is lost.

This is because the data is being sent in a POST format.

In addition, if a user presses F5, or wants to go back then a message comes up -- which means a POST processing file has to be in place to prevent issues.

I've looked around the forum, and there are several threads on the same subject, such as:
http://ellislab.com/forums/viewthread/71061/

There appears to be several ways around this -- but which one is the right one?

1) enable_query_strings = TRUE
outputs: (example.com/index.php?c=controller&m=function)

2) use sessions.
Problem: I'm coding it for a very big site and I don't like the idea of storing things in sessions or cookies for security and memory issues.

3) store all searches in a table, use a session to reference it.
Problem: If there's loads of searches performed by lots of people on a traffic heavy website.

4) Use URI base64 encoding/decoding.

5) Use URI segments which tell CI what to do with it.

I'm leaning towards 1, but this will sacrifice clean FURLS -- I'm wondering:

>> What's the right way to do it (assuming someone's already cracked it)?

Thanks.
#2

[eluser]simshaun[/eluser]
Why not put the search keyword in the URL in a clean format? The only drawback I can see to this is that you'll need to make sure the illegal url characters in the config are set correctly
1. POST to site.com/search/terms/
2.
Code:
class Search...
function terms($terms = NULL)
{
    if ($terms === NULL):
        if ($this->input->post('terms') !== FALSE && $this->input->post('terms') != ''):
            redirect('search/' . urlencode($terms));
            exit;
        else:
            // Action if $this->input->post('terms') is empty
        endif;
    else:
        if ($terms == ''):
            // Action if $terms is empty
        endif;
    endif;

    // Do search
}
Kinda in a hurry to come up with that, but the logic should be correct.
If someone searches for "some search string", the url becomes site.com/search/terms/some+search+string
Then he could bookmark the url and come back at will, without doing the search again.
#3

[eluser]gh0st[/eluser]
I'll try it... failing that, I'll probably go with the enable_query_strings = TRUE

If anyone else has a better idea, or has solved this problem in the past, I'd love to hear how you did it.
#4

[eluser]gh0st[/eluser]
Hi there.

You're answer does work - which is great, but it comes back with an error relating to the urlencode, it outputs the result in a /terms/some+search+query.
#5

[eluser]maesk[/eluser]
Hi gh0st,

I had the same problem and one of the solutions I tried was #4 (base64encoding) but then I had problems with disallowed characters in the URL and also the clean URLs are compromised.

I ended up rewriting the pagination class (storing it under system/application/libraries/Pagination_by_post.php) so it works with POST. The links to the different pages then become submit buttons and the values for the query string, current page number, total rows - and in my case also which field has been searched - are stored in hidden form fields. So each time you click on a link for subsequent or previous pages it's like a new form submit.

It works very well. I think there should be a built-in option to use pagination this way.
#6

[eluser]gh0st[/eluser]
Hi there.

I don't the idea of using POST unless its a really big form with lots and lots of fields to pass back and forth, for a simple search, or a form with 5 or 6 fields I think POST is un-necessary.

In addition it causes all sorts of problems if the user presses back or F5 whilst with GET you don't have these issues.

Whilst the answer provided by "simshaun" does work, I opted to go with using query strings, because .. well even Google uses the GET, so I don't see why I can't either.

This involved changing;

Code:
$config['uri_protocol']    = "PATH_INFO";
$config['enable_query_strings'] = TRUE;

And a lot of hacking in my controller/model.

I did get it up and run, so that my urls look like this when I do a search;

Code:
http://ci.localhost/holidays/search/?destination=all&day=9&month=02&year=2009&flexibility=2&duration=1&departAirport=all&... etc

I know its not SEO, but it was the only way to get it to work.

Personally I'm not happy with the way CI handles search forms using a strict POST method.

It took me hours to figure out to get it working properly, and now I realise my client wants more refinement and sort ordering options... and so forth.

It makes you wonder if CI is meant to help, or just in the way.




Theme © iAndrew 2016 - Forum software by © MyBB