Prevent the mail from sending if fields are empty

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 4
Joined: Mon Apr 28, 2014 7:04 pm

Prevent the mail from sending if fields are empty

Postby bbowman1170 » Wed Apr 30, 2014 7:00 am

I am trying to make form items required for a registration form. The basic code works fine. It sends the registration email, but how do I make it fail to send mail if fields are left blank? I am clearly VERY new to php. Please assist.

$field_name = $_POST['cf_name'];
$field_age = $_POST['cf_age'];
$field_phone = $_POST['cf_phone'];
$field_address = $_POST['cf_address'];
$field_city = $_POST['cf_city'];
$field_state = $_POST['cf_state'];
$field_zip = $_POST['cf_zip'];
$field_grade = $_POST['cf_grade'];
$field_ename = $_POST['cf_ename'];
$field_ephone = $_POST['cf_ephone'];

$mail_to = '';

$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'Age: '.$field_age."\n";
$body_message .= 'Grade: '.$field_grade."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Address: '.$field_address."\n";
$body_message .= 'City: '.$field_city."\n";
$body_message .= 'State: '.$field_state."\n";
$body_message .= 'ZIP: '.$field_zip."\n";
$body_message .= 'Emergency Contact: '.$field_ename."\n";
$body_message .= 'Emergency Phone Number: '.$field_ephone;

$headers = 'From: '.$field_name."\r\n";
$headers .= 'Reply-To: '.$field_."\r\n";

$mail_status = mail($mail_to , $subject, $body_message, $headers);
$mail_error = 'null';

if(empty($field_name) || empty($field_phone) || empty($field_age)) { echo $mail_error; ?>

<script language="javascript" type="text/javascript">
alert('Submition failed. Please make sure you have filled out all categories. If you did this, and see this message, send an email to');
window.location = 'testreg.html';

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'testreg.html';
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to');
window.location = 'testreg.html';
<?php }
} ?>

To clarify. The code functions fine and does distinguish between the required fields being complete or not. This is evident because of 2 different submit messages(using a javascript popup). The only frustration is that is sends an email No Matter What! How do I get it to fail to send if there is 1 or more missing fields?

php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Prevent the mail from sending if fields are empty

Postby seandisanti » Wed Apr 30, 2014 8:20 am

I posted to your other thread before seeing this one, but re-reading reminded me, you should make sure to have an exit; when fail condition is done, so code stops executing.

Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 9 guests