Unholy Prayer Posted January 17, 2007 Share Posted January 17, 2007 I am making the hosting application for my website and I want to put a question that says Do you agree to the terms of service and then they check it. How do I make it so that the checkbox must be checked in order for them to submit the form? Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 17, 2007 Share Posted January 17, 2007 Javascript form validation. It's usually a pretty good idea to follow-up with PHP form validation [code=php:0]if(!$_POST['checkbox']) echo "you didn't check the box!";[/code]on the other side (just incase they don't have javascript turned on).[url=http://www.phpfreaks.com/quickcode/JavaScript-Simple-Form-Validation-Script/276.php]Javascript form validation tutorial[/url] Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted January 17, 2007 Author Share Posted January 17, 2007 Ok, I got it to display if the checkbox is not checked, but it still inserts the info into the database. This is my code:[code]<? include('styles/default/header.tpl');include('styles/default/navigation.tpl');?><td class="content"><table align="center" cellspacing="1" cellpadding="1" border="0"><tr><form action="hosting.php" method="POST"><td align="center" colspan="2">Apply for Hosting</td></tr><tr><td align="right">Your Username:</td><td align="left"><input type="text" name="username" size="30"></tr><tr><td align="right">Email Address:</td><td align="left"><input type="text" name="email" size="30"></tr><tr><td align="right">Site Name:</td><td align="left"><input type="text" name="site_name" size="30"></td></tr><tr><td align="right">Site Topics:</td><td align="left"><input type="text" name="topics" size="30"></td></tr><tr><td align="center" colspan="2">Do you agree to the <a href="termsofservice.php">Terms of Service</a>?<input type="checkbox" name="tos" value="yes">Yes</td></tr><tr><td align="center" colspan="2"><input type="submit" value="submit" name="submit"></form></td></tr><tr><?phprequire_once('config.php');//initilize PHPif(isset($_POST['submit'])) { $username = $_POST['username']; $email = $_POST['email']; $site_name = $_POST['site_name']; $topics = $_POST['topics']; $tos = $_POST['tos']; MYSQL_QUERY("INSERT INTO hosting_apps (username,email,site_name,topics)". "VALUES ('$username', '$email', '$site_name', '$topics')") or die (mysql_error()); echo "<td align='center' colspan='2'>Thank you for your application. We will review it and will make a decision within the next couple of days.</td></tr>"; if(empty($tos) || trim($tos) == "") { echo "You must agree to the Terms of Service before you can apply for hosting. If you have not read them, you can do so by clicking <a href='termsofservice.php'>here</a>."; } }include('styles/default/footer.tpl');?>How do I make it so that the checkbox must be checked in order to insert the info into the database?[/code] Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 17, 2007 Share Posted January 17, 2007 you're just echoing text -- and you're doing it after the mysql query. how about this:[code]<?phpif($tos) {MYSQL_QUERY("INSERT INTO hosting_apps (username,email,site_name,topics)". "VALUES ('$username', '$email', '$site_name', '$topics')") or die (mysql_error());} else {echo "You forgot to accept the terms!";} ?>[/code] Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted January 19, 2007 Author Share Posted January 19, 2007 What do I replace with that? Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 19, 2007 Share Posted January 19, 2007 [code=php:0]if($tos) { //if the checkbox returns a value (checked)mysql_query // and everything else you want to happen if they check the box} else { //if the checkbox does not return a value (not checked)echo "you forgot to check the box"; //and anything else the scipt should do if the box is not checked}[/code] Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted January 19, 2007 Author Share Posted January 19, 2007 Ok, I think I'm making progress. When the checkbox isn't checked, it doesn't insert the info into the db, but it still displays the thank you message. Quote Link to comment Share on other sites More sharing options...
Crimpage Posted January 19, 2007 Share Posted January 19, 2007 Where is the thankyou message kept?What you are trying to do is this... Im not going to give you the whole code, just the steps you want to take:[code]if ($tos) // Box is checked, all good{ do the mysql command show the thank you message whatever else you want to do ONLY IF THE BOX IS CHECKED} else{ // Box not checked... bad person... BAD! Show message saying they didn't check the box, please press back... or whatever}See how the thank you message is only shown IF the check the box... [/code] Quote Link to comment 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.