Thaikhan Posted August 18, 2013 Share Posted August 18, 2013 Hey guys, I'm struggling to find why my checkbox isn't posting as set. I would really appreciate it if someone could tell me what I'm doing wrong. Here's the form: $form = "<form action='./register.php' method='post'> <table> <tr> <td></td> <td><font color='red'>$errormsg</font></td> </tr> <tr> <td>Username:</td> <td><input type='text' name='user' value='$getuser' /></td> </tr> <tr> <td>Email:</td> <td><input type='text' name='email' value='$getemail' /></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='pass' value='' /></td> </tr> <tr> <td>Retype Password:</td> <td><input type='password' name='retypepass' value='' /></td> </tr> <tr> <td>Receive Forum Notifications:</td> <td><input type='checkbox' name='fn' value='1' /></td> </tr> <tr> <td></td> <td><input type='submit' name='registerbtn' value='Register' /></td> </tr> </table> </form>"; And here's the beginning of the page: <?php error_reporting(E_ALL ^ E_NOTICE); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Member System - Register</title> <link href="./style.css" rel="stylesheet" type="text/css" /> </head> <body> <?php if ( $_POST['registerbtn'] ) { $getuser = $_POST['user']; $getemail = $_POST['email']; $getpass = $_POST['pass']; $getretypepass = $_POST['retypepass']; if (isset($_POST['fn'])) { echo "checked"; exit(); } else echo "unchecked"; exit(); Thanks so much!! Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/ Share on other sites More sharing options...
Barand Posted August 18, 2013 Share Posted August 18, 2013 You have a pair of {} missing in the final else statement Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445615 Share on other sites More sharing options...
Thaikhan Posted August 18, 2013 Author Share Posted August 18, 2013 My problem is that it's not setting the variable. Any ideas? if ( $_POST['registerbtn'] ) { $getuser = $_POST['user']; $getemail = $_POST['email']; $getpass = $_POST['pass']; $getretypepass = $_POST['retypepass']; if (isset($_POST['fn'])) { $fn == 1; } else { $fn == 0; } echo $fn; exit(); Nothing is being echoed... Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445618 Share on other sites More sharing options...
Barand Posted August 18, 2013 Share Posted August 18, 2013 do you have error reporting turned on? Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445619 Share on other sites More sharing options...
Thaikhan Posted August 18, 2013 Author Share Posted August 18, 2013 There's no mysql_error and the browser isn't coming up with anything. Neither is the server log. Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445625 Share on other sites More sharing options...
jazzman1 Posted August 18, 2013 Share Posted August 18, 2013 There's no mysql_error and the browser isn't coming up with anything. Neither is the server log. No, that's impossible or you have a logical error somewhere in the script. My briefly test just works fine: <?php error_reporting(-1); if (!empty($_POST['registerbtn'])) { $getuser = $_POST['user']; $getemail = $_POST['email']; $getpass = $_POST['pass']; $getretypepass = $_POST['retypepass']; if (isset($_POST['fn'])) { echo "checked"; exit(); } } ?> <form action='index.php' method='post'> <table> <tr> <td></td> <td><font color='red'></font></td> </tr> <tr> <td>Username:</td> <td><input type='text' name='user' value='' /></td> </tr> <tr> <td>Email:</td> <td><input type='text' name='email' value='getemail' /></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='pass' value='' /></td> </tr> <tr> <td>Retype Password:</td> <td><input type='password' name='retypepass' value='' /></td> </tr> <tr> <td>Receive Forum Notifications:</td> <td><input type='checkbox' name='fn' value='1' /></td> </tr> <tr> <td></td> <td><input type='submit' name='registerbtn' value='Register' /></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445633 Share on other sites More sharing options...
Thaikhan Posted August 18, 2013 Author Share Posted August 18, 2013 The script below will echo checked or unchecked accordingly, so it's working, but I don't understand why it's not echoing 0 or 1 for var $fn. if ( $_POST['registerbtn'] ) { $getuser = $_POST['user']; $getemail = $_POST['email']; $getpass = $_POST['pass']; $getretypepass = $_POST['retypepass']; if (isset($_POST['fn'])) { $fn == 1; echo $fn; echo "Checked"; echo mysql_error(); exit(); } else { $fn == 0; echo $fn; echo "Unchecked"; echo mysql_error(); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445634 Share on other sites More sharing options...
jazzman1 Posted August 18, 2013 Share Posted August 18, 2013 (edited) I am not sure whether the checkbox element accepts a value attribute itself, check at docs, but .. entirely sure that the checked attribute can be used to indicate the default state. So, you should set this to either true or false. Edited August 18, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445639 Share on other sites More sharing options...
mac_gyver Posted August 18, 2013 Share Posted August 18, 2013 (edited) two == is a comparison operator. your code is not assigning a value to $fn (one =), it's comparing $fn with 1 or 0 and if you had php's error reporting turned full on, you would know this because you would be getting an undefined variable error at the $fn references. Edited August 18, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445641 Share on other sites More sharing options...
Solution Thaikhan Posted August 18, 2013 Author Solution Share Posted August 18, 2013 Thank you mac_gyver. I thought the server logs were giving me everything but I guess I was wrong. Thanks so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/281301-i-dont-know-why-my-checkbox-isnt-working/#findComment-1445642 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.