Jump to content

Looking for a better way to handle check boxes


perky416

Recommended Posts

Hi guys

 

On the edit profile page on my website, im trying to get is so that when the user first opens the page, if the database field reads "1" the checkbox is checked. However if the user unchecks it and makes a mistake on the form which returns an error, the checkbox remains unchecked. Also if the user unchecks the text box, the 1 in the database is changed to a 0. And vise versa.

 

The only way i have got it to work succesfully is by using the following:

 

<?php
$test = $_POST['test'];
$submit = $_POST['submit'];
if (!isset($test))
{
	$test="0";
}
else
{
	$test="1";
}

if ($submit)
{

mysql_query("UPDATE profiles SET test='$test' WHERE username='user'");
echo "ok";
}
?>
<html>
<form action="test.php" method="POST">
<input type="checkbox" name="test" <?php if ($submit) { if ($test=="1") { echo "checked='checked'";}} else { if ($row['test'] == "1") {echo "checked='checked'";}} ?> />
<input type="submit" name="submit" />
</form>
</html>

 

My problem is, i have about 60 check boxes on the page, and i dont really want to write the code below for all of them:

if (!isset($test))
{
$test="0";
}
else
{
$test="1";
}

 

Does anybody know a better way to do this so i dont end up with 420 lines of extra code?

 

Many thanks.

Link to comment
Share on other sites

Yes, unchecked checkboxes are never submitted. You could, however, make an array with all checkbox names, create the checkboxes out of that array and than compare the $_POST["boxes"] array with the one you have. What I mean is this:

<form action="/" method="post">
<?php
$checkboxnames = array("chkbx1", "chkbx2", "chkbx3", "chkbx4", "chkbx5"); //all 60 names
$checkboxvalues = array();

if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach ($checkboxnames as $chkbx) {
	if (in_array($chkbx, array_keys($_POST["boxes"]))) {
		$checkboxvalues[$chkbx] = 1;
	} else {
		$checkboxvalues[$chkbx] = 0;
	}
}
/*
$checkboxvalues will look like this:
Array
(
	[chkbx1] => 1
	[chkbx2] => 0
	[chkbx3] => 0
	[chkbx4] => 0
	[chkbx5] => 1
)
*/
}

foreach ($checkboxnames as $name) {
$checked = "";
if (isset($_POST["boxes"][$name])) {
	$checked = ' checked="checked"';
}
echo $name.': <input type="checkbox" name="boxes['.$name.']" value="1"'.$checked.' /><br />';
}
?>

<input type="submit" value="submit" />
</form>

Link to comment
Share on other sites

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.