mdvignesh Posted January 20, 2012 Share Posted January 20, 2012 Checkbox value has to inserted into database Also I have JavaScript check box validation I don't know what to give name for checkbox register.php <html> <head> <script type="text/javascript"> function regval() { //name validation var name_val=document.registration.uname.value; if(name_val=="") { alert("enter first name"); document.registration.uname.focus(); return false; } else if(name_val!='') { var name_re =/^[-A-Za-z. ]*$/; if(name_re.test(name_val) == false) { alert("enter valid name"); document.registration.uname.focus(); return false; } } //email validation var mail_val=document.registration.email1.value; if(mail_val=="") { alert("enter email address"); document.registration.email1.focus(); return false; } else if(mail_val!="") { var mailcheck = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(mailcheck.test(mail_val)==false) { alert("enter valid email address"); document.registration.email1.focus(); return false; } } //city validation var city_val=document.registration.ucity.value; if(city_val=="") { alert("enter city name"); document.registration.ucity.focus(); return false; } else if(city_val!='') { var city_re =/^[-A-Za-z. ]*$/; if(city_re.test(city_val) == false) { alert("enter valid city name"); document.registration.ucity.focus(); return false; } } //state validation var state_val=document.registration.ustate.value; if(state_val=="") { alert("enter state name"); document.registration.ustate.focus(); return false; } else if(state_val!='') { var state_re =/^[-A-Za-z. ]*$/; if(state_re.test(state_val) == false) { alert("enter valid state name"); document.registration.ustate.focus(); return false; } } //country validation var country_val=document.registration.ucountry.value; if(country_val=="") { alert("enter country name"); document.registration.ucountry.focus(); return false; } else if(country_val!='') { var country_re =/^[-A-Za-z. ]*$/; if(country_re.test(country_val) == false) { alert("enter valid country name"); document.registration.ucountry.focus(); return false; } } //login name validation var lname_val=document.registration.lname.value; if(lname_val=="") { alert("enter username "); document.registration.lname.focus(); return false; } //password validation var pass_val=document.registration.pass.value; if(pass_val=="") { alert("enter password"); document.registration.pass.focus(); return false; } else if(pass_val!="") { var invalid = " "; var minLength = 6; if (pass_val.length < minLength) { alert("Your password must be at least " + minLength + " characters long"); document.registration.pass.focus(); return false; } if (pass_val.indexOf(invalid) > -1) { alert("Sorry, spaces are not allowed."); document.registration.pass.focus(); return false; } } //checkbox if (document.registration.chk[0].checked == false && document.registration.chk[1].checked == false && document.registration.chk[2].checked == false) { alert ("select checkbox"); return false; } } </script> </head> <body> <?php include("data.php"); if(isset($_POST["reg"])) { $name=$_POST['uname']; $mail=$_POST["email1"]; $city=$_POST['ucity']; $state=$_POST['ustate']; $country=$_POST['ucountry']; $uname=$_POST['lname']; $passwd=$_POST['pass']; $inr=mysql_real_escape_string(implode(",",$_POST["chk"])); if(mysql_query("insert into register(name,email,city,state,country,username,password,interest) values ('$name','$mail','$city','$state','$country','$uname','$passwd','$inr')")) $good="You have successfully registered,you can login now"; else $bad="error:".mysql_error(); } mysql_close($cn); ?> <br /> <strong style="background-color:#00CCFF"> REGISTRATION FORM</strong> <br /><br /> <form action="register.php" method="post" name="registration"> <table border="0" width="600" height="600" align="left"> <tr><td>Name:</td><td><input type="text" name="uname"/></td></tr> <tr><td>Email:</font></td><td><input type="text" name="email1" /></td></tr> <tr><td>City:</font></td><td><input type="text" name="ucity" /></td></tr> <tr><td>State:</td><td><input type="text" name="ustate" /></td></tr> <tr><td>Country:</td><td><input type="text" name="ucountry" /></td></tr> <tr><td>Username:</td><td><input type="text" name="lname" /></td></tr> <tr><td>Password:</td><td><input type="password" name="pass" /></td></tr> <tr><td>Intrest: <input name="chk[]" id="chk[]" type="checkbox" value="reading" />Reading <input name="chk[]" id="chk[]" type="checkbox" value="sports" />Sports <input name="chk[]" id="chk[]" type="checkbox" value="travel" />Travel </td></tr> <tr><td> <input type="submit" name="reg" value="ok" style="cursor:pointer;" onclick="return regval();" /> <?php if(isset($good)) {?> <input type="button" value="Login" onclick="javascript:window.location='loginpage.php'" style="cursor:pointer;" /><br /><br /><?php }?> </td></tr> </table> <?php if(isset($good)) echo $good; if(isset($bad)) echo $bad; ?></strong></font> </form> </div> </body> </html> 17377_.php Quote Link to comment https://forums.phpfreaks.com/topic/255401-insert-checkbox-value-to-database/ Share on other sites More sharing options...
scootstah Posted January 20, 2012 Share Posted January 20, 2012 So what's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/255401-insert-checkbox-value-to-database/#findComment-1309473 Share on other sites More sharing options...
mdvignesh Posted January 20, 2012 Author Share Posted January 20, 2012 Not able to validate checkbox If no checkbox is selected I need alert message Quote Link to comment https://forums.phpfreaks.com/topic/255401-insert-checkbox-value-to-database/#findComment-1309474 Share on other sites More sharing options...
Pikachu2000 Posted January 20, 2012 Share Posted January 20, 2012 This has nothing to do PHP. Moving to Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/255401-insert-checkbox-value-to-database/#findComment-1309478 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.