Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

preserving checkboxes across the pages in php

Posted on 2011-03-24
8
Medium Priority
?
365 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 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 35215869
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 111

Expert Comment

by:Ray Paseur
ID: 35216419
Thanks for the points and best of luck with it, ~Ray
0
 
LVL 34

Expert Comment

by:Slick812
ID: 35216688
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35217212
@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
 
LVL 34

Expert Comment

by:Slick812
ID: 35217496
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
ID: 35217548
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 111

Expert Comment

by:Ray Paseur
ID: 35218846
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 34

Expert Comment

by:Slick812
ID: 35223064
@ 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

824 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