Solved

preserving checkboxes across the pages in php

Posted on 2011-03-24
8
341 Views
Last Modified: 2012-05-11
Hi I have php pagination program which  retrieves one page at a time from the database. First column is
checkboxes. How to preserve their value when users moves to the next page (GET requess)

The records per page are optional so it can be very long. So I am interested in doing it in the most efficient way
 
0
Comment
Question by:Rozamunda
  • 4
  • 3
8 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
Use session_start() at the top of all of the scripts involved.

Copy the checkbox information into the $_SESSION array and recover the checkbox information from the $_SESSION array.

As you do this, be aware that unchecked checkboxes do not appear in the request at all - they are not present and empty like an empty text input control; they are simply missing from the request.

Does that help answer the question?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and best of luck with it, ~Ray
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
greetings. Rozamunda, , using a next page as a GET request may be a problem, as far as I know there is no info gathered from the check boxes and sent with the page request , as usually  
index.php?page=2
so you may have to use javascript and get all of the checked boxes and send that with the page request. maybe liike -
index.php?page=2&check=0-2-3-5-7-11-14-23-24-31

this uses a numerical listing of the checked boxes , and when you do the call in PHP to rewrite the checkboxes, from a database array, you check and see if the numerical position of the checkbox is in the returned array and write 'checked' in the <input> tag for the checkbox

$check = $_GET['check'];
$checkAry = explode('-', $check);

for ($- = 0; $i < $ckCount; ++$i) {
// other code here to write <input type=checkbox +other stuff
if (in_array("".$i, $checkAry)) echo "checked";
}

but your methods may not allow this, it's hard to know what you may be doing in your code, from the info you have given so far. . . .
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@Slick812:  I had in mind something like this.  All the best, ~Ray
<?php // RAY_checkboxes_session.php
error_reporting(E_ALL);
session_start();

// INITIALIZE SOME TEST DATA - THIS MIGHT COME FROM A DATA BASE
if (!isset($_SESSION['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_SESSION['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_SESSION['box3'])) $_SESSION['box3'] = FALSE;

// PROCESS THE POST DATA
if ( isset($_GET['box1'])) $_SESSION['box1'] = TRUE;
if ( isset($_GET['box2'])) $_SESSION['box2'] = TRUE;
if ( isset($_GET['box3'])) $_SESSION['box3'] = TRUE;

if (!isset($_GET['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_GET['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_GET['box3'])) $_SESSION['box3'] = FALSE;

// TELL WHAT WAS CHECKED
if ($_SESSION['box1']) echo 'YOU CHECKED box1<br/>';
if ($_SESSION['box2']) echo 'YOU CHECKED box2<br/>';
if ($_SESSION['box3']) echo 'YOU CHECKED box3<br/>';

// CREATE THE FORM
$form = <<<EOFORM
<form method="get">
CHECK OR UNCHECK SOME BOXES HERE<br/>
<input name=box1 type="checkbox" /><br/>
<input name=box2 type="checkbox" /><br/>
<input name=box3 type="checkbox" /><br/>
<input type="submit" value="go" />
</form>
EOFORM;

// REPLACE THE FORM FIELDS TO INDICATE THE CHECKED VALUES IN THE SESSION
if ($_SESSION['box1']) $form = str_replace('box1', 'box1 checked', $form);
if ($_SESSION['box2']) $form = str_replace('box2', 'box2 checked', $form);
if ($_SESSION['box3']) $form = str_replace('box3', 'box3 checked', $form);

// PRESENT THE FORM
echo $form;

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 33

Expert Comment

by:Slick812
Comment Utility
thanks for your time Ray, but as it's seems to be the usual thing, you or I do not have enough knowledge of what the methods or problems may be. LOL, if only I had taken up mind reading instead of computer coding, I did not mean to post after question was answered, but I started the comment while I was eating lunch (free time I have to use  here), and posted after eating lunch,
0
 

Author Comment

by:Rozamunda
Comment Utility
Hi Slick, thanks for you reply

I am going to use jquery to read through checkobixes and put selected items into the array.
_GET I need to go to the next page, and then when I return I can check if any of the items
were selected by looking into my array of selected items which I am going to store in the _SESSION

The problem I can possible have is what if user selects on the page like  10000 items ( I have the option to
store pages that long). Can I just store them in the _Session ?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You can store any number of things in the Session array.  Or you can store them in a data base, which might be a smarter design, since a client who makes 10,000 selections, only to find that the session timed out and the data was lost will not be a client any more!
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
@ Rozamunda
I hope you can take my next statements as an effort to help and not to criticize your methods.
I also think that a session can store that amount of data.
But people are not computers, and to have more than 200 (or even 100) displayed form data entry inputs (checkboxes, text, radios, selections) is more than I would want to deal with, or even could deal with, and have a clear Idea of what I was doing and remembering what the different data sets were for. I can not imagine being presented with 500 form inputs, much less 10000. Maybe you could have them process One page of 200 check boxes at a time? But if this does not apply for your users, then do not comment about it, and talk about any other help you might need.

While ray prefers a database storage, I usually go for a file storage, for a non "lookup or indexed" info store, , or an SQLite file database when only accessed from one page on the site. I think the default for session data, is to be stored in a file in the temp directory. The PHP serialize() function makes array storage in a file relativity easy. But there are advantages and disadvantages to both.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now