Benjamin Lu

everyday is cool

Build a Contact Form That Keeps Your Input

An error message is easier to act on when the form still contains what you typed. You can test that with a small local PHP form containing an email field and a message field.

On submit, validate the email and check that the message is present. If either check fails, render the form again with a message next to the affected field. Escape submitted values before rendering them back into HTML. For this exercise, display a confirmation on success and leave actual email delivery out of the lab.

Enter an invalid address and a two-line message. Submit, fix the address, and submit again. The message should still be there after the first attempt. Also try input containing quotes and angle brackets to check that it appears as text. Those tests catch problems that a single successful submission can miss.

PHP manual on handling forms