15.3. Redisplaying a Form After PHP Validation FailsWhile you intend for JavaScript to catch errors up front, before the user has navigated away from the page through the form submission, there will be times when PHP catches an error. When this happens, an informative error message displays and the form is redisplayed that had a validation problem. When redisplaying the form, it's a much smoother user experience if the data the user submitted is pre-populated in the form. There's nothing worse than filling out a page-long form only to find out there's a missing checkbox, meaning you have to start over. We'll modify our previous example to check whether a username is already present in the users table, as shown in Example 15-4. Example 15-4. Displaying an error from PHP and redisplaying the form with submitted values
If a user enters invalid data, as shown in Figure 15-3, she'll get the response shown in Figure 15-4. If the data is correct, she'll see the response in Figure 15-5. Figure 15-3. The form before submission with a conflicting usernameFigure 15-4. After form submission, the error displays and the form repopulatesFigure 15-5. A successful submission |