Watkins Web 2: Blog

Exercise 19: PHP Forms in Action

Download Exercise 19

Start by viewing the live demo form and comparing it to the demo-input.php file in the download. Then try submitting the two forms in it repeatedly with different options.

Once you’ve got an idea of what’s going on, do the same with the exercise itself. View the live version and compare it to your PHP files. To complete the exercise, edit exercise-output.php on your desktop and upload both it and exercise-input.php to your FTP space on the web.

Some tips:

  • Make small, incremental changes and test them on the server before moving on to the next step. Nothing’s more frustrating than writing a lot of code which then produces a blank page: that way you have to meticulously go backwards (removing lines of code) until you find the culprit.
  • If you can’t tell what variable has what value at a certain point in the code, echo it ! You can prefix these kinds of lines with DEBUG to make sure you don’t forget to go back and remove them at the end.
  • Most PHP errors have to do with semicolons or curly braces in the wrong place. Remember, a straightforward PHP statement always ends in a semicolon:
    echo "My favorite color is $color_variable.";
    But a conditional, because the statement is hypothetical, has the semicolon inside the curly brace at the end:
    if ($color_variable) {echo "My favorite color is $color_variable.";}