Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

HTML Forms and PHP

  • Print
  • + Share This
This chapter is from the book

Displaying Errors

After you install PHP on a Web server, it will run under a default configuration with respect to security, how it handles data, performance, and so forth. Since this book was first written, three significant changes have been made in that default configuration, and all PHP programmers should be aware of them. The first has to do with whether error messages are displayed.

PHP can be set to give you feedback if it encounters problems. Whether it does so is based on the display_errors setting in PHP's configuration file (which is called php.ini). In current versions of PHP, this setting is disabled by default, so errors generated by a script result in a blank screen. You may have experienced this result in the previous example.

To have PHP display errors, you can

  • Turn display_errors back on (see the "Configuring PHP" section of Appendix A, "Installation and Configuration," for more information)
  • Turn display_errors back on for a particular script.

Although the first option is best for people learning PHP, it's only an option for those with administrative control over the server. Anyone can use the second option by including this line in your script:

ini_set('display_errors', 1);

The ini_set() function allows a script to temporarily override a setting in PHP's configuration file. In this example, you'll turn the display_errors setting to on, which is represented by the number 1.

To display errors:

  1. Open handle_form.php in your text editor.
  2. As the first line of PHP code, enter the following (Script 3.5):

    ini_set('display_errors',1);

    Again, this line tells PHP you'd like to see any errors that occur. You should call it first thing in your PHP section so the rest of the PHP code will abide by this new setting.

  3. Save the file as handle_form.php.
  4. Upload the file to your Web server and test it in your Web browser (Figures 3.11 and 3.12).

    Figure 3.11

    Figure 3.11 Trying the form again...

    Figure 3.12

    Figure 3.12 ...and now any error messages are displayed.

    If the resulting page has no errors in it, then the script will run as it did before. If you saw a blank page when you ran the form earlier, you should now see messages like those in Figure 3.12. If so, keep reading this chapter to find the solutions.

Script 3.5. Turning on the display_errors directive should eliminate blank pages that occur when an error is found.

1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
    1.0 Transitional//EN"
2   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
    transitional.dtd">
3   <html xmlns="http://www.w3.org/1999/xhtml">
4   <head>
5        <meta http-equiv="content-type"
         content="text/html; charset=
         iso-8859-1" />
6        <title>Your Feedback</title>
7   </head>
8   <body>
9   <?php // Script 3.5 - handle_form.php
    (second version after Script 3.4)
10
11  ini_set ('display_errors', 1); // Let me
    learn from my mistakes.
12
13  // This page receives the data from
    feedback.html.
14  // It will receive: title, name, email,
    response, comments, and submit.
15
16  print "Thank you $title $name for your
    comments. <br />";
17  print "You stated that you found this
    example to be $response and added:
    $comments";
18
19  ?>
20  </body>
21  </html>
  • + Share This
  • 🔖 Save To Your Account