Solved

preserving checkboxes across the pages in php

Posted on 2011-03-24
8
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 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 110

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 110

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 110

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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…

626 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