Twister1004 Posted December 6, 2008 Share Posted December 6, 2008 Hey, I'm having a bit of a problem. I'm trying to get a check box to work 2 ways. When the user registers, I'm trying to get the checkbox to work as a DEFAULT as 0 or if it is checked, it will be set to 1. Objective: Check box is not working correctly. I want it to equal 0 if its not checked, but I want it to set to 1 when its checked. Issue: It doesn't want to set to the default (which in the database is set to 0 for the default). It either works as "1" or it just says it can't work. <?php session_start(); error_reporting(E_ALL); include("config.php"); ?> <html> <head> <title>FBLA | Allumni Registration</title> <link href="style.css" rel="stylesheet" type="text/css" /> <style> body{ background:#fdd017; background-image:url("images/test2.png"); background-repeat:repeat-x; color:#FFFFFF; } .body{ position:absolute; top:24px; left:39px; width:900px; height:700px; border: solid #FFFFFF; } #box{ text-align:right; } </style> </head> <body> <div class="body"> <table> <form method="POST" action=""> <tr><td> Name: </td><td> <input type="text" name="name" maxlength="30" /></td></tr> <tr><td> Password:</td><td> <input type="password" name="pass" maxlength="25" /> </td></tr> <tr><td> Verify Password: </td><td> <input type="password" name="pass2" maxlength="25" /> <tr><td> Age: </td><td> <input type="text" name="age" maxlength="2" /></td></tr> <tr><td> E-mail: </td><td> <input type="text" name="email" maxlength="35" /><td></tr> <tr><td> Verify E-mail: </td><td> <input type="text" name="vemail" maxlength="35" /></td></tr> <tr><td> Sex: </td><td> <input type="radio" name="sex" value="Male" />Male <input type="radio" name="sex" value="Female" />Female</td></tr> <tr><td> Year Graduated: </td><td> <input type="text" name="date" maxlength="4" /></td></tr> <tr><td id="box"><input type="checkbox" name="activities" /></td><td> Yes, I would like updates on future FBLA activities.</td></tr> <tr><td> <input type=submit name=submit value="Register" /> </td></tr> </form> <?php if(isset($_POST['submit'])){ $name = mysql_real_escape_string($_POST['name']); $pass = mysql_real_escape_string($_POST['pass']); $pass2 = mysql_real_escape_string($_POST['pass2']); $password = hash('sha512' , $pass); $age = mysql_real_escape_string($_POST['age']); $sex = $_POST['sex']; $email = mysql_real_escape_string($_POST['email']); $email2 = mysql_real_escape_string($_POST['vemail']); $date = mysql_real_escape_string($_POST['date']); $activities = $_POST['activities']; $ip = $_SERVER['REMOTE_ADDR']; // users ip is stored in the variable ip /*$sel = 'SELECT * FROM `allumni` WHERE `name`="' . $_POST['name'] . '"'; $result = mysql_query($sel) or die(mysql_error()); $data = mysql_fetch_assoc($result); if(mysql_num_rows($result) >0){ echo "The username currently exists. Please select another username."; exit(); }*/ if($name == ""){ echo "You did not enter a username. Please re-enter your username."; exit(); } elseif($password == ""){ echo "You did not enter a password. Please re-enter your password."; exit(); } elseif($pass != $pass2){ echo "Your passwords do not match! Please check your passwords."; exit(); } elseif($age == ""){ echo "You did not enter your age. Please re-enter your age."; exit(); } elseif($email == ""){ echo "You did not enter your E-mail. Please re-enter your E-mail."; exit(); } elseif($email2 != $email){ echo "Your E-mails do not match. Please check your E-mails."; exit(); } elseif($date == ""){ echo "You did not enter your graduation year. Please re-enter your graduation year."; exit(); } elseif($sex == ""){ echo "You need to enter your gender."; exit(); } else{ $sql = ('INSERT INTO `allumni` (name, password_sha512, age, sex, email, date_registered, year_grad, get_updates, ip) VALUES '. '("'.$name.'", "'.$password.'", "'.$age.'", "'.$sex.'", "'.$email.'", NOW(), "'.$date.'", "'.$activities.'", "'.$ip.'" )'); $result = mysql_query($sql); if($result){ echo "Your account was created successfully!"; } else{ echo "Your account was NOT created due to an error. Please report this to the Mrs. Dochety or Mr. King " . mysql_error(); } } } ?> </table> </div> </body> </html> Any help, hints, tips, reading, posting, etc would be helpful! Thanks =) Link to comment https://forums.phpfreaks.com/topic/135762-solved-kinda-html-but-also-php/ Share on other sites More sharing options...
timmah1 Posted December 6, 2008 Share Posted December 6, 2008 I personally would do this On your form <input name="activities" type="checkbox" value="1" /> Then the POST if($_POST['activities'] = "1"){ $activities = "1"; } else { $activities = "0"; } Link to comment https://forums.phpfreaks.com/topic/135762-solved-kinda-html-but-also-php/#findComment-707385 Share on other sites More sharing options...
mrdamien Posted December 6, 2008 Share Posted December 6, 2008 ... <tr><td id="box"><input type="checkbox" name="activities" value="1" /></td><td> Yes, I would like updates on future FBLA activities.</td></tr> ... <?php ... $activities = (int)$_POST['activities']; ... ?> Using (int) casts the value to the integer type. (It forces it to be a number). If the checkbox is selected, the value passed will be '1'. If the checkbox is not selected, the value is not passed at all (ie, isset($_POST['activities']) == false) (int) '1' == 1 and (int) null == 0 @timmah1 You missed an equals sign in your IF statement. Link to comment https://forums.phpfreaks.com/topic/135762-solved-kinda-html-but-also-php/#findComment-707386 Share on other sites More sharing options...
timmah1 Posted December 6, 2008 Share Posted December 6, 2008 Ah yes, I see that now. I like your way better anyhow Thanks Link to comment https://forums.phpfreaks.com/topic/135762-solved-kinda-html-but-also-php/#findComment-707391 Share on other sites More sharing options...
Twister1004 Posted December 6, 2008 Author Share Posted December 6, 2008 Thank you very much. It helped me. I've seen the (int) But, I've never seen it used before. That will actually come in handy now. =). although, I'm getting a notice, it wont matter since it forces it anyways XP. Thank you very much! Link to comment https://forums.phpfreaks.com/topic/135762-solved-kinda-html-but-also-php/#findComment-707401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.