Printer Friendly Version

BellaOnline's PHP Editor

Personal Activities Calendar Program - Missing Form Data In A PHP Program

So far in the Personal Activities Calendar Program we have written the code for the possibility that the Activity Entry web form has not yet been submitted ($_POST['submit'] is empty). If this is the case, the program will initializes the variables to null just before the function call that will display the form in the web browser for the first time. Now we will code for the possibility that the form has been submitted ($_POST['submit'] is not empty). If this is the case, there are two options for the next step.
  1. If the form was submitted and the user has not supplied all the required data, the program will display the form in the web browser again along with an error message.

  2. If the form was submitted and the required data is not missing, the program will process the data and send it to the database.
Let's work on the first option. How will we test if there is any missing data? First, we need to decide which data is required and which is optional. For this simple calendar program, the only required data is the title of the activity and the starting time/date. The other input fields in the web form are optional. These are the five required data input fields in the web form and the six optional.



Our first step is to use the PHP if statement to test for any missing data for each of the required and optional input fields. We will also create a variable called $error_message and initially set the value to no. If there is any missing data, the variable will be set to yes. Here is the first section of the code.

$error_message = "no";

$error_message = "yes";
$pass_act_title = "";
else { $pass_act_title = $_POST['act_title']; }

if(empty($_POST['act_desc'])) { $pass_act_desc = ""; }
else { $pass_act_desc = $_POST['act_desc']; }
. . .
. . .

What does this code do? The program will first set the $error_message variable to no. It will next test each of the eleven data input fields passed from the form. In the code above, we are testing for the first two variables act_title and act_desc. However, the entire code will test for the other 9 variables as well. There are three possible results.

  1. If the program encounters missing data from a required input field (act_title), it will set the corresponding parameter variable to null and the $error_message variable to yes.

  2. If the program encounters missing data from an optional data field (act_desc), it will set the parameter variable to null but not set the $error_message to yes.

  3. If data for an input field is not missing, it will perform the else statement. It will set the parameter variable to the value of the corresponding $_POST[''] variable and the $error_message variable will not be affected.
If the program makes it through the test for all eleven $_POST variables and the $error_message variable is still set to no, this tells the program that no required data is missing. In this case, the data will be processed further and passed to the database. However, if the $error_message variable has been set to yes, this means that at least one required field has missing data. The program will call the Activity_Entry_Form function again. Because we have already set the value of the parameter variables to null (for missing data) or to the value of the corresponding $_POST variable, we are ready to pass them with the function call.

if ($error_message == "yes")
Activity_Entry_Form( $pass_act_title, $pass_act_desc,
$pass_act_month_1, $pass_act_day_1, $pass_act_year_1,
$pass_act_hour_1, $pass_act_month_2, $pass_act_day_2,
$pass_act_year_2, $pass_act_hour_2, $pass_act_status );
else { process data further }
Note--The arrow indicates that the code is wrapped to a second line and should really be all on one line.

The next step will be to make adjustments to the Activity_Entry_Form function to handle the new error message.

Note--The purpose of this tutorial is to build a Personal Activities Calendar Program for personal use and teach the basics of PHP and MySQL databases. I have not included any security features such as preventing malicious input data. That would make this tutorial too complicated for the novice. If you were creating a program for the web, you would want to include such security in your program code.

This site needs an editor - click to learn more!

PHP Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Editor Wanted for details.

| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2018 Minerva WebWorks LLC. All rights reserved.

BellaOnline Editor