limitphp Posted November 3, 2008 Share Posted November 3, 2008 I have a checkbox in a form. How do I determine if its checked or not with php? The $_POST['checkboxname']; seems to get all values, except for the checkbox. Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/ Share on other sites More sharing options...
Flames Posted November 3, 2008 Share Posted November 3, 2008 because you have multiple check boxes with the same id and if its ticket then $_POST['checkbox'] should have those values, not to sure how to seperate the values though? Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/#findComment-681397 Share on other sites More sharing options...
laffin Posted November 3, 2008 Share Posted November 3, 2008 CheckBoxes either return the value or dun so ya can use isset to determine the value $checkbox = isset($_GET['mycheckbox'])?1:0; 1 if it was checked 0 if it wasnt Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/#findComment-681402 Share on other sites More sharing options...
rhodesa Posted November 3, 2008 Share Posted November 3, 2008 if the checkboxes all have different names, then $_POST['checkboxname'] should be set to the value or to 'on' if no value was used. if you want to use the same name, you should use it like so: <input type="checkbox" name="chkbox[]" value="val1" /> <input type="checkbox" name="chkbox[]" value="val2" /> <input type="checkbox" name="chkbox[]" value="val3" /> and in PHP, $_POST['chkbox'] should be an array of which values you selected Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/#findComment-681404 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2008 Share Posted November 3, 2008 Only checked checkboxes are returned to PHP. Please post the source for your form and the script that's processing it. Ken Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/#findComment-681405 Share on other sites More sharing options...
limitphp Posted November 3, 2008 Author Share Posted November 3, 2008 CheckBoxes either return the value or dun so ya can use isset to determine the value $checkbox = isset($_GET['mycheckbox'])?1:0; 1 if it was checked 0 if it wasnt CheckBoxes either return the value or dun so ya can use isset to determine the value $checkbox = isset($_GET['mycheckbox'])?1:0; 1 if it was checked 0 if it wasnt Ok, I'm sorry, let me clear this up...I miswrote ....I use $_POST to get all form items successfully. My inputtexts. I only have one checkbox (if you used multiple...wouldn't you use radio buttons instead?). The $_POST on the checkbox doesn't seem to get any value. I put in your code and now all it returns is a 0, even when its checked. Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/#findComment-681407 Share on other sites More sharing options...
limitphp Posted November 3, 2008 Author Share Posted November 3, 2008 $rememberMe = isset($_POST['remember'])?1:0; . . . <SCRIPT> alert('<?php echo $rememberMe ?>'); </script> . . . <FORM NAME="frmLogin" METHOD="post" ACTION="index.php"> <INPUT TYPE="checkbox" NAME="remember"> </FORM> All I'm seeing now on the alert box is a 0. Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/#findComment-681410 Share on other sites More sharing options...
limitphp Posted November 3, 2008 Author Share Posted November 3, 2008 Nevermind, I'm sorry. I was closing the FORM before the checkbox. <?php if ($loginExist==0){ ?> <FORM NAME="frmLogin" METHOD="post" ACTION="index.php"> Username<INPUT TYPE="TEXT" NAME="username" SIZE="15"> Password<INPUT TYPE="password" NAME="password" SIZE="15"> <INPUT TYPE="hidden" NAME="mode" VALUE="1"> <INPUT TYPE="submit" VALUE="Login"> </FORM> <?php }else{ ?> <?php echo $fname ?><br> <?php } ?> Register | Our Mission <?php if ($loginExist==0){ ?> <INPUT TYPE="checkbox" NAME="remember">Remember Me <?php } ?> I fixed it and now it works. <?php if ($loginExist==0){ ?> <FORM NAME="frmLogin" METHOD="post" ACTION="index.php"> Username<INPUT TYPE="TEXT" NAME="username" SIZE="15"> Password<INPUT TYPE="password" NAME="password" SIZE="15"> <INPUT TYPE="hidden" NAME="mode" VALUE="1"> <INPUT TYPE="submit" VALUE="Login"> <?php }else{ ?> <?php echo $fname ?><br> <?php } ?> Register | Our Mission <?php if ($loginExist==0){ ?> <INPUT TYPE="checkbox" NAME="remember">Remember Me </FORM> <?php } ?> I guess thats why you say post your source code.... I just didn't want to post all of it because its a pretty big page. Thank you guys...and sorry for the misinformation on my part. Quote Link to comment https://forums.phpfreaks.com/topic/131246-solved-php-and-checkboxes/#findComment-681415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.