ngreenwood6 Posted August 17, 2008 Share Posted August 17, 2008 I have a question about forms and php playing nice together. I am trying to do some error handling. If a user does not put in their username for it to say "please enter your username" next to the text box. I have been able to accomplish this in two ways. By submitting the form to itself and making an exact duplicate of the original page with the error next to the text box and if the user does not enter a username redirecting them to that page. I am thinking that there is an easier way to do this. I have to submit a form from one page ("index.php") to another page ("submit.php"). Does anyone know how I can accomplish this? If you have any questions feel free to ask. Thanks Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted August 17, 2008 Share Posted August 17, 2008 Can you show your code Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 17, 2008 Share Posted August 17, 2008 You could use JavaScript to validate forms client-side http://www.w3schools.com/jS/js_form_validation.asp Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 17, 2008 Author Share Posted August 17, 2008 I would rather stick with php to validate my forms because I will be using variables from mysql as well. I do not know a whole lot about javascript. index.php <form method="post" action="submit.php"> <table> <tr> <td>Username:</td> <td><input name="username" type="text"></td> </tr> <tr> <td>Password:</td> <td><input name="password" type="password"></td> </tr> <tr> <td><input name="submit" type="submit" value="submit"></td> </tr> </table> </form> submit.php <?php $username = $_POST['username']; $password = $_POST['password']; if (empty($username)) { echo "You must enter a username"; } elseif (empty($password)) { echo "You must enter a password"; } else { echo "You are logged in"; } ?> When I submit this without entering a username or password it takes me to a blank page where it gives me the errors. I want it to just stay on the main page and give me the errors next to the boxes where you enter the information. I can add the following page and edit the submit page as follows: username.php <form method="post" action="submit.php"> <table> <tr> <td>Username:</td> <td><input name="username" type="text"></td> You must enter a username! </tr> <tr> <td>Password:</td> <td><input name="password" type="password"></td> </tr> <tr> <td><input name="submit" type="submit" value="submit"></td> </tr> </table> </form> submit.php <?php $username = $_POST['username']; $password = $_POST['password']; if (empty($username)) { echo "You must enter a username"; } elseif (empty($password)) { include("username.php"); } else { echo "You are logged in"; } ?> When I do that it will show the error message next to the box. But if I do that with every type of error that would take me forever and there would be alot of pages. I know there has to be a better way. Please someone help. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 17, 2008 Author Share Posted August 17, 2008 I know everyone does this somehow, so how would you do it if you were in my situation? Anyone? Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted August 17, 2008 Share Posted August 17, 2008 One way i do it <?php if(isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; if (empty($username)) { echo "You must enter a username"; } elseif (empty($password)) { echo "You must enter a password"; } else { echo "You are logged in"; } } ?> <form method="post" action=""> <table> <tr> <td>Username:</td> <td><input name="username" type="text"></td> </tr> <tr> <td>Password:</td> <td><input name="password" type="password"></td> </tr> <tr> <td><input name="submit" type="submit" value="submit"></td> </tr> </table> </form> I think this will work Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted August 17, 2008 Share Posted August 17, 2008 one way i do it is to gather the errors in an array, and then display them using a foreach construct. it'll give ya something to work with maybe. <?php if ($_POST['username']=='' || strlen($_POST['username'])==FALSE){ $errors[] = 'Please enter a username.'; } if ($_POST['password']=='' || strlen($_POST['password'])==FALSE){ $errors[] = 'Please enter a password.'; } //display errors if set; if(is_array($errors)){ echo "<form action='yourfile.php' method='POST'>"; echo '<b>The following errors occured.</b><br />'; while (list($key,$value) = each($errors)){ echo $value; } echo "</form>"; }else{ // if no errors, do something; } Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 18, 2008 Author Share Posted August 18, 2008 mrmarcus your coding looks a little confusing and I don't think that it will accomplish what I am trying to. It will show this error on a new page. When all I want to do basically is echo the errors on the page where the form will be filled out. Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 18, 2008 Share Posted August 18, 2008 When all I want to do basically is echo the errors on the page where the form will be filled out. That's exactly what JavaScript is best at. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 18, 2008 Share Posted August 18, 2008 JavaScript is a possibility but not the best one. Remember, people can disable javascript. It should not be the only form of error checking. In fact, if you merged what mrMarcus and Blade280891 have, you will get what you want. <?php //Start the validation. //Remember, the form posts to this page again! if(isset($_POST['submit'])){ //The form has been submitted $errors = array(); //make a new array called errors. //now lets get all the post values $username = $_POST['username']; $password = $_POST['password']; //First check if username has a value. if($username == ""){ //They didn't enter a username. Add an error! $errors[] = "You did not enter a username!"; //The $errors[] is making a new element in the array. } //Do the same for password if($password == ""){ $errors[] = "You did not enter a password!"; } //Now, the next line checks to see if there were any errors. if(!empty($errors)){ //This is checking to see if $errors is NOT empty. So if we encountered an error, we will show them! //We have some errors. Let's show them! echo "Sorry but we found some errors!<ol>"; foreach($errors as $error){ //this foreach loop takes $errors and turns each element into the variable $error echo "<li>$error</li>"; } echo "</ol>"; //So now the errors are shown in an ordered list. //Continue with the script, and show the form again! }else{ //No error encountered. Log the user in then redirect! $_SESSION['bleh'] = $username; header("Location: control.php"); exit; } } //Even if we got here and the form was submitted but it had errors. The errors will be shown above the form! ?> <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <table> <tr> <td>Username:</td> <td><input name="username" type="text"></td> You must enter a username! </tr> <tr> <td>Password:</td> <td><input name="password" type="password"></td> </tr> <tr> <td><input name="submit" type="submit" value="submit"></td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted August 18, 2008 Share Posted August 18, 2008 Some times i use something similar to fears, but just use a var instead of an array , and just echo it , also check if the var is present and if it is , dont run the query. 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.