Last active
August 29, 2015 14:01
-
-
Save anoxic/11dd6cacfbeafbaac330 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if(!empty($_POST)) | |
| { | |
| // This is the error message you will display if something goes wrong. For now this will be empty: | |
| $error = ""; | |
| // Wrap everything inside a "try" block so that you can display | |
| // the error message but still also render your form. | |
| try | |
| { | |
| // All of your error checking will be the same | |
| // you will just need to use `throw new Exception("error message")` | |
| // instead of `die("error message")` | |
| if(empty($_POST['username'])) | |
| { | |
| throw new Exception("Please enter a username."); | |
| } | |
| if(empty($_POST['password'])) | |
| { | |
| throw new Exception("Please enter a password."); | |
| } | |
| // You will need to include everything that should happen if all goes correctly | |
| // within this "try" block, including the redirect back to the login page | |
| } | |
| catch (Exception $e) | |
| { | |
| // Set the error message to something you can display later: | |
| $error = $e->getMessage(); | |
| } | |
| } | |
| ?> | |
| <!-- Display an error message like so --> | |
| <?php if (!empty($error)): ?> | |
| <div class="error">We ran into the following problem: <?php echo $error; ?></div> | |
| <?php endif; ?> | |
| <!-- You can refill your form inputs like so, adding a value based on the request --> | |
| <input type="text" name="username" value="<?php echo $_POST['username']; ?>"> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For http://stackoverflow.com/questions/23661672/how-to-have-this-form-be-validated-on-the-same-page