/    Sign up×
Community /Pin to ProfileBookmark

PHP, Checkboxes and Sessions

Ok, I’ve run into a slight problem
I’ve got a long list of objects, so much that I must paginate them for any kind of easy reading.

But now, I’m trying to add a compare functionality that will let me select a number of objects. When the ‘compare’ button is clicked, it will list the objects next to each other on a new page.

I do this by adding any checked objects to a session variable after the page is submitted. If the next or previous button is pressed, it submits the form to the page with the next 20 or so objects.

I can add the items fine, but my problem comes in when I want to go back and remove an object from the session variable after it’s already there.

This is because, if you deselect a checkbox and resubmit the form, it is no longer in the $_POST array.

Does anyone have any ideas on how to remove a deselected object from the session array?

If you need more info, I’ll be happy to provide it.

Jaelan

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 13.2005 — I think I'd use a sub-array within the $_SESSION array, and just unset all of its elements and then repopulate it for each set checkbox. E.g., if the inputs were sent with an array name such as this...

[code=html]
<input type="checkbox" name="check[]" value="item1">
<input type="checkbox" name="check[]" value="item2">
<input type="checkbox" name="check[]" value="item3">
[/code]

...then each time I processed a submit I'd simply do:
[code=php]
if(isset($_SESSION['check']))
{
foreach($_SESSION['check'] as $key => $val)
{
unset($_SESSION['check'][$key]);
}
}
if(isset($_POST['check']))
{
foreach($_POST['check'] as $val)
{
$_SESSION['check'][] = $val;
}
}
[/code]
Copy linkTweet thisAlerts:
@JaelanauthorSep 13.2005 — Thanks for the reply NogDog.

What I ended up doing was placing all object id's into a seperate array and passing it as a hidden field.

This way I have the possible values for that page and which ones were checked.

I did run into a problem w/ passing an array as a hidden field. What I ended up doing is imploding the $everything array, and then exploding it after the form submitted.

Jaelan
Copy linkTweet thisAlerts:
@NogDogSep 14.2005 — There's almost always more than one way to do it. ?
×

Success!

Help @Jaelan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...