tags:

views:

207

answers:

3

On a site that I'm working on, I'm displaying results from a database query. Each page shows 15 results, so if there are 45 results returned from the query, they will be displayed on 3 separate pages, each containing 15 results.

I want the user to be able to select any number of items from the first page, then switch to the other pages and select items from those pages as well, while still remembering the items that were selected on the first page. (Eventually, when the user has selected all the items, he will click a Submit button.) So if the user navigates back to the first page, the items that he checked should show up as being selected. On pages that I've seen on the Web, when the user navigates back to the first page, all the checkboxes are cleared again (which is not what I want).

Any ideas on the best way to achieve this? Thanks.

+1  A: 

You could do many things.

  • Save as a JavaScript cookie
  • Save as a session by using AJAX on each click
  • Hide each page as a "tab" so they are actually there but just not shown.
Ólafur Waage
I like the tab idea, but from experience I've found it doesn't scale very well after a few pages.
TheTXI
all of those require javascript and don’t degrade gracefully
Maciej Łebkowski
+2  A: 

use hidden inputs to carry over the results from the previous set

<!-- hidden fields -->
<input type='hidden' name='whatever[]' value='whatever'/>

<!-- checkboxes -->
<input type='checkbox' name='whatever[]' value='whatever'/>
Jonathan Fingland
+5  A: 

Remembering the selections in-between pages means you have some state that needs storing somewhere. There are a variety of ways to do it, but it boils down to one of these...

  1. Keep it on the client (e.g. cookies)
  2. Keep it on the server (e.g. in a database)
  3. Keep sending it backwards and forwards (e.g. hidden form field, as in ASP.NET viewstate)

A common abstraction offered by platforms such as PHP and ASP.NET is the idea of "session". The actual implementation of session state might be some combination of those 3 possibilities above, and it probably offers the easiest route. I suggest you look at session state in PHP.

Martin
+1 for Session. You can build an array of selected items and store it in Session. Just remember to clear it when you are done.
Rob Allen
Go with option 3 - the other options can lead to race conditions.
David Dorward
Very helpful, and pretty much what I thought I'd have to do. Thanks!
johnnyb10