Skip to content
Advertisement

Ignore empty form fields when sending PHP

I’m fairly new to PHP so I understand enough to get myself confused, but not enough to accomplish anything productive. I have a sign-up form that allows the user to add up to five students. The form fields for each student is wrapped in their own div class (.student1, ,student2, .student3, .student4, and .student5). I’m trying to prevent the PHP code from sending data if all the fields in the wrapped div classes are left blank.

Example 1: If the user fills out information for two students (.student1 and .student2), only email those two students and prevent the other three from sending. Example 2: If the user fills out only partial information for a student, still send ALL fields for that student to email and ignore the other students that are entirely empty.

I would be ok with implementing code that ignored ALL empty form fields from the entire form but I would rather only apply the rule to all fields within a div. Is that even possible!?

Here is my code:

<?php
/* Converts the multiple checkboxs arrays into strings. */
if($_SERVER["REQUEST_METHOD"] == "POST")     {
    foreach($_POST as $key => $val) {
        if(is_array($_POST[$key])) $_POST[$key] = implode('<br>', $_POST[$key]);
    }
}
?>

<?php

if(isset($_POST['submit'])) {

/* POC Info */
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$location = $_POST['location'];
$referral = $_POST['referral'];

/* Student 1 */
$first1 = $_POST['1first'];
$last1 = $_POST['1last'];
$age1 = $_POST['1age'];
$program1 = $_POST['1program'];
$message1 = $_POST['1message'];
$interests1 = $_POST['1interests'];
/* Student 2 */
$first2 = $_POST['2first'];
$last2 = $_POST['2last'];
$age2 = $_POST['2age'];
$program2 = $_POST['2program'];
$message2 = $_POST['2message'];
$interests2 = $_POST['2interests'];
/* Student 3 */
$first3 = $_POST['3first'];
$last3 = $_POST['3last'];
$age3 = $_POST['3age'];
$program3 = $_POST['3program'];
$message3 = $_POST['3message'];
$interests3 = $_POST['3interests'];
/* Student 4 */
$first4 = $_POST['4first'];
$last4 = $_POST['4last'];
$age4 = $_POST['4age'];
$program4 = $_POST['4program'];
$message4 = $_POST['4message'];
$interests4 = $_POST['4interests'];
/* Student 5 */
$first5 = $_POST['5first'];
$last5 = $_POST['5last'];
$age5 = $_POST['5age'];
$program5 = $_POST['5program'];
$message5 = $_POST['5message'];
$interests5 = $_POST['5interests'];

/* Defines the reciever, sender, and email subject */
$to = "email@clientdomain.com";
$sender = $email;
$subject = "New sign-up request from $name";

/* Defines email headers (from, cc, bcc, reply to, etc) and ensures email body is displayed in HTML format */
$headers = "From: $senderrn" . "Reply-To: $senderrn" . "Content-type: text/htmlrn" . "X-     Mailer: PHP/" . phpversion();

/* Defines the content of the HTML in the email body */
$email_content = <<<EOD
<strong style="color: red;">Point of Contact</strong><br>
Name: $name <br>
Phone: $phone <br>
Email: $email <br>
Location: $location <br>
Referral: $referral <br><br>
<strong style="color: red;">First Student Information</strong><br>
Name: $first1 $last1 <br>
Age Group: $age1 <br>
Program: $program1 <br>
<strong>Interests and Goals</strong><br>
$interests1 <br>
<strong>Additional Information</strong><br>
$message1 <br><br>
<strong style="color: red;">Second Student Information</strong><br>
Name: $first2 $last2 <br>
Age Group: $age2 <br>
Program: $program2 <br>
<strong>Interests and Goals</strong><br>
$interests2 <br>
<strong>Additional Information</strong><br>
$message2 <br><br>
<strong style="color: red;">Third Student Information</strong><br>
Name: $first3 $last3 <br>
Age Group: $age3 <br>
Program: $program3 <br>
<strong>Interests and Goals</strong><br>
$interests3 <br>
<strong>Additional Information</strong><br>
$message3 <br><br>
<strong style="color: red;">Fourth Student Information</strong><br>
Name: $first4 $last4 <br>
Age Group: $age4 <br>
Program: $program4 <br>
<strong>Interests and Goals</strong><br>
$interests4 <br>
<strong>Additional Information</strong><br>
$message4 <br><br>
<strong style="color: red;">Fifth Student Information</strong><br>
Name: $first5 $last5 <br>
Age Group: $age5 <br>
Program: $program5 <br>
<strong>Interests and Goals</strong><br>
$interests5 <br>
<strong>Additional Information</strong><br>
$message5 <br><br>
EOD;


/* Successful pop up message */
echo 
"<script>alert('Congratulations on taking your first step! A member of our team will get in touch     with you as soon as possible.');</script>
<script>window.location = 'http://www.clientdomain.com/success.html'</script>";

/* mail syntax is: reciever, subject, email content, and headers which are all defined above) */
mail($to, $subject, $email_content, $headers);


} else {

/* Failed pop up message */
echo
"<script>alert('Sorry, there seems to be an error with your submission.');</script>
<script>window.location = 'http://www.clientdomain.com/fail.html</script>";
}

?>

Advertisement

Answer

You can actually simplify a lot of what you have. Remember, keep it DRY (don’t repeat yourself).
Also, I am checking for empty values so you don’t get errors for undefined indexes as well as giving a default value if it isn’t there.
On top of that, trimming everything so you aren’t left with a bunch of spaces before/after anything

Instead of creating 30 variables (6 for each student), create 1 array, then loop through the array at a later time. Helps keep it expandable in the future if you want to add more fields or students to your form.

The only time a student’s info isn’t added to a form, is when ALL fields are empty. If 1 only 1 is used, give a default to the others, if they are empty.

<?php
if (isset($_POST['submit'])) {

    /* POC Info */

    $name=isset($_POST['name'])?trim($_POST['name']):null;
    $name=empty($name)?'Default POC Name':$name;
    $phone=isset($_POST['phone'])?trim($_POST['phone']):null;
    $phone=empty($phone)?'Default POC Phone':$phone;
    $email=isset($_POST['email'])?trim($_POST['email']):null;
    $email=empty($email)?'Default POC Email':$email;
    $location=isset($_POST['location'])?trim($_POST['location']):null;
    $location=empty($location)?'Default POC Location':$location;
    $referral=isset($_POST['referral'])?trim($_POST['referral']):null;
    $referral=empty($referral)?'Default POC referral':$referral;

    $students=array();
    for ($i=0; $i<=5; $i++) {
        $first=trim($_POST[$i.'first']);
        $last=trim($_POST[$i.'last']);
        $age=intval($_POST[$i.'age']);
        $program=trim($_POST[$i.'program']);
        $interests=trim($_POST[$i.'interests']);
        $message=trim($_POST[$i.'message']);

        if (empty($first)&&empty($last)&&empty($age)&&empty($program)&&empty($message)&&empty($interests)) continue;
        $students[]=array(
            'first'=>empty($first)?'unknown':$first,
            'last'=>empty($last)?'unknown':$last,
            'age'=>empty($age)?'unknown':$age,
            'program'=>empty($program)?'unknown':$program,
            'message'=>empty($message)?'unknown':$message,
            'interests'=>empty($interests)?'unknown':$interests,
        );
    }

    /* Defines the reciever, sender, and email subject */
    $to="email@clientdomain.com";
    $sender=$email;
    $subject="New sign-up request from $name";

    /* Defines email headers (from, cc, bcc, reply to, etc) and ensures email body is displayed in     HTML format */
    $headers="From: $senderrn"."Reply-To: $senderrn"."Content-type: text/htmlrn"."X-     Mailer: PHP/".phpversion();

    /* Defines the content of the HTML in the email body */
    $email_content=<<<EOD
<strong style="color: red;">Point of Contact</strong><br>
Name: $name <br>
Phone: $phone <br>
Email: $email <br>
Location: $location <br>
Referral: $referral <br><br>
EOD;
$student_num=0;
foreach ($students as $student) {
    $email_content.='
<strong style="color: red;">Student '.++$student_num.' Information</strong><br>
Name: '.$student['first'].$student['last'].'<br>
Age Group: '.$student['age'].'<br>
Program: '.$student['program'].'<br>
<strong>Interests and Goals</strong><br>
'.$student['interests'].' <br>
<strong>Additional Information</strong><br>
'.$student['message'].' <br><br>
';
}
?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement