Jump to content

Checkbox processing help - how to find the unchecked box?


Recommended Posts

I have a registration form that includes 5 checkboxes as an array:

 

X Session1

X Session2

  Session3

X Session4

X Session5

 

all set up as name="sid[]" array. If the user unchecks Session 3, I want to update the database, setting all the checked sessions to 'e' (enroll) and the one unchecked session to 'u' (unenroll).

 

When I submit, I want to update the database for ALL the Sessions, setting the Status to 'e'  (enroll) or 'u' (unenroll). This is what I tried:

 

$count = 0;
foreach ($sidAll as $temp) {
if ($sid[$count] == NULL) {
	print "<br />UPDATE senroll SET status = 'e' WHERE sid = '$sid[$count]' <br>";
} else {
	print "<br />UPDATE senroll SET status = 'u' WHERE sid = '$sid[$count]' <br>";
}
$count++;
}

 

It compares the user selected sid array with the database sid array. It doesn't work though, because the user selected sid contains an undefined element - the sid that was unchecked.

 

Any help?

Here's the HTML, which is really generated by PHP:

 

<?php
<input type="checkbox" value="1" name="sid[]" checked>Monday (2008-03-18)<br />
<input type="checkbox" value="2" name="sid[]" checked>Tuesday (2008-03-19)<br />
<input type="checkbox" value="3" name="sid[]" checked>Wednesday (2008-03-20)<br />
<input type="checkbox" value="4" name="sid[]" checked>Thursday (2008-03-22)<br />
<input type="checkbox" value="5" name="sid[]" checked>Friday (2008-03-23)<br />

<input type="hidden" value="5" name="sessionCount">
<input type="hidden" value="1" name="sidAll[]">
<input type="hidden" value="2" name="sidAll[]">
<input type="hidden" value="3" name="sidAll[]">
<input type="hidden" value="4" name="sidAll[]">
<input type="hidden" value="5" name="sidAll[]">
?>

 

The form submits to a sql.php page where I run this:

 

<?php
$sidAll = $_POST['sidAll'];
$sid = $_POST['sid'];
$sessionCount = $_POST['sessionCount'];

//count the number of sessions selected
$sessionCountSelected = count($sid);
//if the number selected and number defined differ, then update the session enrollment table
if ($sessionCount != $sessionCountSelected) {
//cycle through ALL the sessions defined and determine which ones were DESELECTED
$count = 0;
foreach ($sidAll as $temp) {
	if (!in_array($sid[$count])) {
		print "<br />UPDATE senroll SET status = 'e' WHERE sid = '$sid[$count]' <br>";
	} else {
		print "<br />UPDATE senroll SET status = 'u' WHERE sid = '$sid[$count]' <br>";
	}
	$count++;
}
}
?>

 

which of course isn't working. It can't read an undefined element in the array, so I get an error.

You can do something like this to pre-initialize the sid[] array:

<?php
if (!empty($_POST)) echo '<pre>' . print_r($_POST,true) . '</pre>';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title></title>
</head>

<body>
<form action="" method="post">
<?php
for($i=1;$i<6;$i++)
echo '<input type="hidden" value="U" name="sid[' . $i . ']">';
?>
<input type="checkbox" value="1" name="sid[1]" checked>Monday (2008-03-18)<br />
<input type="checkbox" value="2" name="sid[2]" checked>Tuesday (2008-03-19)<br />
<input type="checkbox" value="3" name="sid[3]" checked>Wednesday (2008-03-20)<br />
<input type="checkbox" value="4" name="sid[4]" checked>Thursday (2008-03-22)<br />
<input type="checkbox" value="5" name="sid[5]" checked>Friday (2008-03-23)<br />

<input type="hidden" value="5" name="sessionCount">
<input type="hidden" value="1" name="sidAll[1]">
<input type="hidden" value="2" name="sidAll[2]">
<input type="hidden" value="3" name="sidAll[3]">
<input type="hidden" value="4" name="sidAll[4]">
<input type="hidden" value="5" name="sidAll[5]">
<br>
<input type="submit" name="submit" value="Test It">
</form>


</body>
</html>

 

Be warned: I've never seen this method fail, but the order that values are presented to the processing script is not well defined, so a new browser might break this method.

 

Ken

try

<?php
$sidAll = $_POST['sidAll'];
$sid = $_POST['sid'];
//$sessionCount = $_POST['sessionCount'];

//count the number of sessions selected
//$sessionCountSelected = count($sid);
//if the number selected and number defined differ, then update the session enrollment table
//if ($sessionCount != $sessionCountSelected) {
//cycle through ALL the sessions defined and determine which ones were DESELECTED
//$count = 0;
foreach ($sidAll as $temp) {
	if (in_array($temp,$sid)) {
		print "<br />UPDATE senroll SET status = 'e' WHERE sid = '$temp' <br>";
	} else {
		print "<br />UPDATE senroll SET status = 'u' WHERE sid = '$temp' <br>";
	}
//		$count++;
//	}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.