I should preface this by saying that I'm using sessions to keep track of logged-in users.
I have a script that generates several pages of results. I want the user to be able to choose which results he wants to keep by clicking associated checkboxes.
The difficulty is that since the user's results are across several pages, what he has checked in previous pages must be stored somewhere.
One solution would be to save the checkbox data by making the user click a "store checkboxes" button at the bottom of each results page, but that's a clunky user experience. I would rather have the user check what he wants across all pages, then when ready go to a control panel page to view all his checked results.
It looks like I'll have to use a mixture of js (which I have avoided so far) and php.
Any ideas on what best way to go about this?
Need to store checkbox values across results pages
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Your probably best using javascript to do this, although you might want to just save all the results in the session to begin with and then when the user submits to the next page you check to see what he wants to save, and then remove from the session the results that they want to be removed. Then your just left in the end with all the results the user wants held in the session.
i dont see why to use javascript, except for maybe having javascript add the checked="checked" part. but i dont think its very hard to do w/ php
the following works because if the user does not select the checkbox, the browser will not send anything to php for that checkbox name
the following works because if the user does not select the checkbox, the browser will not send anything to php for that checkbox name
Code: Select all
<?php
if (isSet($_POST['checkbox1'])) {
$_SESSION['checkbox1'] = true;
} else {
$_SESSION['checkbox1'] = false;
}
// you could also use ternary style
$_SESSION['checkbox1'] = (isSet($_POST['checkbox1'])) ? true : false;
// but if you have a lot of checkboxes, you might want to use a loop
$checkboxes = array(
'checkbox1',
'checkbox2',
'checkbox3',
);
foreach ($checkboxes as $checkbox) {
$_SESSION[$checkbox] = (isSet($_POST[$checkbox])) ? true : false;
}
// then on another page, if you need to output them
$checked = (empty($_SESSION['checkbox1'])) ? '' : ' checked="checked"';
echo '<input type="checkbox" name="checkbox1"'.$checked.'>';
// or use the loop again
foreach ($checkboxes as $checkbox) {
$checked = (empty($_SESSION['$checkbox'])) ? '' : ' checked="checked"';
echo "<input type="checkbox" name="$checkbox"$checked>\n";
}
?>I appreciate the code, rehfeld.
The pagination routine I wrote populates each page number with a url, so client data is not posted when a user goes from page to page. That's why php alone won't work.
I want a user to be able to select boxes across multiple pages, by navigating with a standard (page: 1,2,3...) pagination routine, instead of submit buttons, then when ready, go to "check out". For example, the yahoo mail address book allows the user to do just that.
The pagination routine I wrote populates each page number with a url, so client data is not posted when a user goes from page to page. That's why php alone won't work.
I want a user to be able to select boxes across multiple pages, by navigating with a standard (page: 1,2,3...) pagination routine, instead of submit buttons, then when ready, go to "check out". For example, the yahoo mail address book allows the user to do just that.
oh ok it makes sense now.
well, you could use javascript to store the checkbox info into a cookie.
then to get it into php, instead of collecting info from $_POST, use $_COOKIE
each time they goto a diff page, you will be able to collect the info,
so it can still work basically the same.
im not very experienced w/ js,
but i suppose a simple onselect event (or whatever event is most suitable)
would be used to trigger a function that sets that checkbox name into thier cookie.
well, you could use javascript to store the checkbox info into a cookie.
then to get it into php, instead of collecting info from $_POST, use $_COOKIE
each time they goto a diff page, you will be able to collect the info,
so it can still work basically the same.
im not very experienced w/ js,
but i suppose a simple onselect event (or whatever event is most suitable)
would be used to trigger a function that sets that checkbox name into thier cookie.
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
downside, not all users allow js so this is not a "good" soultion, not to insult anyone,
you could use javascript to make a messy url, have it so that when a user clicks a checkbox it does something like
linkName.href= previousHref + "&checkboxnumberwhatever=blah"
i dont know the exact js syntax but you should be able to do something like that
you could use javascript to make a messy url, have it so that when a user clicks a checkbox it does something like
linkName.href= previousHref + "&checkboxnumberwhatever=blah"
i dont know the exact js syntax but you should be able to do something like that
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
how bout something like this?
Code: Select all
<html>
<head><title>check the checks</title></head>
<body>
<center><input type='checkbox' name='check1' onClick='var link=link1.href; link1.href=link+"check1=checked&"'>
check this check box then see what the link has changed to.....<br />
<a name='link1' href='blah.php?'>blah.php</a></center>
</body>
</html>