Russia Posted January 2, 2011 Share Posted January 2, 2011 Hey guys I am trying to add form validation type of thing where it checks if the form was submitted with values in all of the fields. This is how it looks. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); include("config.php"); if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']) && isset($_POST['secQ']) && isset($_POST['secA'])) { $username = mysql_real_escape_string($_POST['username']); $secQ = mysql_real_escape_string($_POST['secQ']); $secA = mysql_real_escape_string($_POST['secA']); $password = $_POST['password']; $email = $_POST['email']; if (empty( $_POST['username'] || $_POST['password'] || $_POST['email'] || $_POST['secQ'] || $_POST['secA'] )) { echo "form not fully filled, reshow first form"; } else { if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { echo "<br><br>Valid email address."; mysql_query("INSERT INTO admin (username, password, email, secquestion, secanswer, sendemail) VALUES ( '$username', '$password', '$email', '$secQ', '$secA','1')") or die (mysql_error()); echo "<br>Account created."; } else { echo "<br><br>Invalid email address."; } }// for if empty } ?> <html> <style type="text/css"> label { width:300px; /*Or however much space you need for the form’s labels*/ float:left; } </style> <hr> <form action="register2.php" method="post"> <div class=”field_container”><label>Username</label><input name="username" type="text" /></div> <div class=”field_container”><label>Password</label><input type="password" value="" name="password" /></div> <div class=”field_container”><label>Email</label><input name="email" type="text" /></div> <div class=”field_container”><label>Choose a secret question</label> <select name="secQ"> <option disabled selected value="0">Choose Secret Question</option> <option value="1">What is your middle name?</option> <option value="2">What is your mothers maiden name?</option> <option value="3">What city were you born in?</option> <option value="4">What is your favorite color?</option> <option value="5">What year did you graduate from High School?</option> <option value="6">What was the name of your first boyfriend/girlfriend?</option> <option value="7">What is your favorite model of car?</option> </select> </div> <div class=”field_container”><label>Enter a secret answer</label><input name="secA" type="text" /></div> <div class=”field_container”><label> </label><input type="submit" value="Submit" /></div> </form> </html> I added the: if (empty( $_POST['username'] || $_POST['password'] || $_POST['email'] || $_POST['secQ'] || $_POST['secA'] )) { part to it, but I think I might of done it wrong, so if someone can help me fix the code because no errors are showing up.. Id really appreciate the help! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 if ( empty( $_POST['username']) || empty($_POST['password']) || empty($_POST['email']) || empty($_POST['secQ']) || empty($_POST['secA']) ) { Quote Link to comment Share on other sites More sharing options...
Russia Posted January 2, 2011 Author Share Posted January 2, 2011 What in there did you change? Oh, I didn't think you needed to add empty to every single one unless its in the parentheses. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 Yup, it sure does. Quote Link to comment Share on other sites More sharing options...
Russia Posted January 2, 2011 Author Share Posted January 2, 2011 The page is still white, Look at my code and tell me if I maybe made a mistake with the elseif or if statements please. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); include("config.php"); if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']) && isset($_POST['secQ']) && isset($_POST['secA'])) { $username = mysql_real_escape_string($_POST['username']); $secQ = mysql_real_escape_string($_POST['secQ']); $secA = mysql_real_escape_string($_POST['secA']); $password = $_POST['password']; $email = $_POST['email']; //if (empty( $_POST['username'] || $_POST['password'] || $_POST['email'] || $_POST['secQ'] || $_POST['secA'] )) { if ( empty( $_POST['username']) || empty($_POST['password']) || empty($_POST['email']) || empty($_POST['secQ']) || empty($_POST['secA']) ) { echo "form not fully filled, reshow first form"; } elseif { if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { echo "<br><br>Valid email address."; mysql_query("INSERT INTO admin (username, password, email, secquestion, secanswer, sendemail) VALUES ( '$username', '$password', '$email', '$secQ', '$secA','1')") or die (mysql_error()); echo "<br>Account created."; } elseif { echo "<br><br>Invalid email address."; } }// for if empty } ?> <html> <style type="text/css"> label { width:300px; /*Or however much space you need for the form’s labels*/ float:left; } </style> <hr> <form action="register2.php" method="post"> <div class=”field_container”><label>Username</label><input name="username" type="text" /></div> <div class=”field_container”><label>Password</label><input type="password" value="" name="password" /></div> <div class=”field_container”><label>Email</label><input name="email" type="text" /></div> <div class=”field_container”><label>Choose a secret question</label> <select name="secQ"> <option disabled selected value="0">Choose Secret Question</option> <option value="1">What is your middle name?</option> <option value="2">What is your mothers maiden name?</option> <option value="3">What city were you born in?</option> <option value="4">What is your favorite color?</option> <option value="5">What year did you graduate from High School?</option> <option value="6">What was the name of your first boyfriend/girlfriend?</option> <option value="7">What is your favorite model of car?</option> </select> </div> <div class=”field_container”><label>Enter a secret answer</label><input name="secA" type="text" /></div> <div class=”field_container”><label> </label><input type="submit" value="Submit" /></div> </form> </html> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 It appears you left the old line in there as well. Delete it and see what happens. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 Nevermind, I was using the browser on my phone and didn't notice that you had commented the line out. What you have now is an elseif with no conditions for it. echo "form not fully filled, reshow first form"; } elseif { elseif what? 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.