Forum Moderators: coopster

Message Too Old, No Replies

$ POST checkboxes

Help please

         

rokec

7:15 pm on Jan 14, 2007 (gmt 0)

10+ Year Member



Can someone write me the best, easy and fast way to handle this. I tried eveything, but nothing seem to work...

supermanjnk

4:02 am on Jan 15, 2007 (gmt 0)

10+ Year Member



You are going to need to be a little more specific as to what problems you are experiencing.

cmarshall

4:15 am on Jan 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll bet this is what you're looking for:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<div>
<form id="form1" action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="get"><div>
<input type="checkbox" id="cbx1" value="cbx1" name="my_checkboxes[]" /> Checkbox 1<br />
<input type="checkbox" id="cbx2" value="cbx2" name="my_checkboxes[]" /> Checkbox 2<br />
<input type="checkbox" id="cbx3" value="cbx3" name="my_checkboxes[]" /> Checkbox 3<br />
<input type="checkbox" id="cbx4" value="cbx4" name="my_checkboxes[]" /> Checkbox 4<br />
<input type="submit" onclick="document.getElementById('form1').method='get';return true" value="GET" />
<input type="submit" onclick="document.getElementById('form1').method='post';return true" value="POST" />
</div></form>
<?php
echo "\$_GET: '";
print_r ( $_GET );
echo "<br />\$_POST: '";
print_r ( $_POST );
?>
</div>
</body>
</html>

Note the bracket ([]) characters at the end of the checkbox names.

rokec

4:58 pm on Jan 15, 2007 (gmt 0)

10+ Year Member



I have just one checkbox and i want to check if it is on.

darrengeorge

5:03 pm on Jan 15, 2007 (gmt 0)

10+ Year Member



if($_POST['theCheckbox']) {
//its on
} else {
//it isnt
}

i think... :o)

eelixduppy

5:11 pm on Jan 15, 2007 (gmt 0)



Welcome to WebmasterWorld, darrengeorge! :)

Your solution would work, however if it's not selected the script will generate a Notice. Not too much of a problem, but if you check to see if it was set, you'd be better off ;)


if([url=http://us3.php.net/manual/en/function.isset.php]isset[/url]($_POST['checkbox'])) {
echo 'Set!';
} else {
echo 'Not set!';
}