0% found this document useful (0 votes)
21 views1 page

PHP Solution 5-1: Making Sure Required Fields Aren't Blank

The document describes how to validate required fields in a PHP form: 1. Initialize empty arrays to store any error messages or missing required fields. 2. Only run the email processing script if the form was submitted, by checking if the submit button field is set in the $_POST array. 3. Define email variables like the destination address and subject line to use later in the processing.

Uploaded by

Touati Bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

PHP Solution 5-1: Making Sure Required Fields Aren't Blank

The document describes how to validate required fields in a PHP form: 1. Initialize empty arrays to store any error messages or missing required fields. 2. Only run the email processing script if the form was submitted, by checking if the submit button field is set in the $_POST array. 3. Define email variables like the destination address and subject line to use later in the processing.

Uploaded by

Touati Bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP Solution 5-1: Making Sure Required Fields arent Blank

When required fields are left blank, you dont get the information you need and the user may never get
a reply,
particularly if contact details have been omitted.
Continue using the file from the previous exercise. Alternatively, use contact_02.php from the ch05
folder and
remove _02 from the filename.
1. The processing script uses two arrays called $errors and $missing to store details of
errors and required fields that havent been filled in. These arrays will be used to control
the display of error messages alongside the form labels. There wont be any errors when
the page first loads, so initialize $errors and $missing as empty arrays in the PHP code
block at the top of contact.php, like this:
<?php
include './includes/title.php';
$errors = [];
$missing = [];
?>
2. The email-processing script should run only if the form has been submitted. As Figure 5-2
shows, the $_POST array contains a name/value pair for the Submit button, which is called
send in contact.php. The value of $_POST['send'] will be defined (set) only if the form
has been submitted. So you can use a conditional statement and the isset() function to
control whether to run the processing script. Add the code highlighted in bold to the PHP
block at the top of the page.
<?php
include './includes/title.php';
$errors = [];
$missing = [];
// check if the form has been submitted
if (isset($_POST['send'])) {
// email processing script
}
?>
Note T he name attribute of the Submit button in this form is send. If you give your Submit

button a different name,


you need to use that name.

3. Although you wont be sending the email just yet, define two variables to store the
destination address and subject line of the email. The following code goes inside the
conditional statement that you created in the previous step:
if (isset($_POST['send'])) {
// email processing script
$to = '[email protected]'; // use your own emai

You might also like