Click to See Complete Forum and Search --> : unchecked checkbox value="" on submit?
tjmcd
04-10-2008, 04:26 PM
Hey All,
I find today -- much to my surprise -- that unlike other "empty" form fields (selects, text etc.) that a checkbox
does NOT seem to submit at all if unchecked. That is (in a PHP script anyway)...//the following does NOT return an alert if checkbox is NOT checked upon submit
if($_POST['mycheckbox']){
echo "<script>alert('mycheckbox posted');</script>";
}whereas...//the following DOES return true EVEN THOUGH mytextbox is left blank on submit
if($_POST['mytextbox']){
echo "<script>alert('myTEXTbox posted');</script>";
} Is this right??? What's the deal there?
zingmatter
04-10-2008, 04:54 PM
Form elements like text , textareas, select will return pass a null/empty value if nothing is entered, whereas checkboxes don't. This is because if you have say 3 checkboxes, and only 2 of them are checked, the name/value pair gets send in the form post.
To get round this you either have to use javascript to detected the unchecked boxes and give them a default "unchecked" value like 0 or something, or alternatively you could try using radio buttons (with yes or no options) though that can look a bit rubbish.
If you know what to expect in the page that's reading the form data then you could do something like
if (is_null($_GET["checkbox1"])) {
$chkbox1 = 0;
} else {
$chkbox1 = $_GET["checkbox1"];
}
if (is_null($_GET["checkbox2"])) {
$chkbox2 = 0;
} else {
$chkbox2 = $_GET["checkbox2"];
}
// etc...
tjmcd
04-10-2008, 05:13 PM
Thanks zingmatter,
Don't see yet why name value pairs wouldn't pass just the same like
"name:checkbox1, value:null"
"name:checkbox2, value:X"
"name:checkbox3, value:Y" ...
but I'll take it on faith (and expreience now) that they just DON'T. Hmmmm....
Anyway, I was thinking I would pass a hidden inuput instead setting the value by javascript onClick of the checkbox (actually have written it as such already), but I think I like your suggestion with the is_null($_GET..) even better still. I'll check it out.
if (is_null($_GET["checkbox1"])) {
$chkbox1 = 0;
} else etc...
zingmatter
04-10-2008, 05:33 PM
I think this is because a web form might have loads of checkboxes on it so rather than post .../myform.php?chk1=&chk2=&chk3=aValue&chk4=&chk5=& and so on, the post only contains the one with a value.
It's causes all kinds of problems if you need to know what wasn't checked, especially if you're deali0ng with dynamic forms where you don't know what to expect from the form post.
felgall
04-10-2008, 05:34 PM
If you want to pass one value if a checkbox is checked and a different value if it is unchecked you need to code the HTML as:
<input type="hidden" name="myckbox" value="unchecked">
<input type="checkbox" name="myckbox" value="checked">
Then the myckbox field (or whatever name you use) will always be passed and will have one of the two values depending on whether the checkbox is or isn't checked. Those values can be whatever you like.
zingmatter
04-10-2008, 05:57 PM
Hey, that's a great idea, I never thought of that! Nice one.
Thanks.
zingmatter
04-10-2008, 06:58 PM
Well I just tested it...
unchecked you get querystring:
testchk.html?myckbox=unchecked
check the box and you get:
testchk.html?myckbox=unchecked&myckbox=checked
Having said that...
if you run the code
<?php
$c = $_GET["myckbox"];
?>
<html>
<body>
<form name="f1" action="testchk.php" method="get">
<input type="hidden" name="myckbox" value="unchecked">
<input type="checkbox" name="myckbox" value="checked">
<input type="submit" value="send">
<br><br>
Value of mychk: <?php echo $c; ?> !
</form>
</body>
</html>
the value of $c is just checked, as the second value of $_GET["mychbox"] is used (i.e. "checked")
felgall
04-10-2008, 10:11 PM
Which browser produces that result? Browsers are supposed to ignore form fields where a subsequent form field has the same name and they are not defined as an array.
zingmatter
04-11-2008, 03:37 AM
In both IE6 and FireFox: this is the php file a ran (local IIS)
<?php
$c = $_GET["myckbox"];
?>
<html>
<body>
<form name="f1" action="testchk.php" method="get">
<input type="hidden" name="myckbox" value="unchecked">
<input type="checkbox" name="myckbox" value="checked">
<input type="submit" value="send">
<br><br>
Value of mychk: <?php echo $c; ?> !
</form>
</body>
</html>
zingmatter
04-11-2008, 03:46 AM
Things doing the same thing in ASP, the value of querystring("myckbox") is a comma delimited list:
<%
Dim c
c = request.querystring("myckbox")
%>
<html>
<body>
<form name="f1" action="testchk.asp" method="get">
<input type="hidden" name="myckbox" value="unchecked">
<input type="checkbox" name="myckbox" value="checked">
<input type="submit" value="send">
<br><br>
Value of mychk: <%= c %> !
<br>
<br>
<%
if len(request.querystring("myckbox")) > 0 then
for each i in request.querystring
response.write i & " = " & request.querystring(i) & "<br>"
next
end if
%>
</form>
</body>
</html>
zingmatter
04-11-2008, 03:59 AM
Add this into the php version of the code and you see the $_GET[] only contains one value.
<?
foreach ($_GET as $key => $val) {
echo $key." = ".$val;
}
?>
So that's odd then. Good news for php development, not so good for classic Asp.
JasonPresley
04-14-2011, 11:13 AM
When a web form is processed and two or more form fields have the same name the value is not that of the last one. They don't overwrite each other. They append to each other. Thus if you submit two form fields both with the name of ckbox one with the value of unchecked and one with the value of checked what you get as a result in the passed form parameters for that field is "unchecked,checked".