James
James

Reputation: 13

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 protected]";
$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: $sender\r\n" . "Reply-To: $sender\r\n" . "Content-type: text/html\r\n" . "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>";
}

?>

Upvotes: 0

Views: 4486

Answers (3)

Oberst
Oberst

Reputation: 477

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 protected]";
    $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: $sender\r\n"."Reply-To: $sender\r\n"."Content-type: text/html\r\n"."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>
';
}
?>

Upvotes: 1

anstue
anstue

Reputation: 1090

You could just check for example if the last name is filled out and include the html-code or not, like this(the point before the = is important, it appends to the string):

$email_content = <<<EOD
    <strong style="color: red;">Point of Contact</strong><br>
    Name: $name <br>
    Phone: $phone <br>
    Email: $email <br>
    <br>
    Location: $location <br>
    Referral: $referral <br><br>
    <strong style="color: red;">First Student Information</strong><br>
EOD;

if(!empty($last1)) {
    $email_content .= <<<EOD
    Name: $first1 $last1 <br>
    Age Group: $age1 <br>
    Program: $program1 <br>
    <strong>Interests and Goals</strong><br>...
EOD;

}
...

Upvotes: 0

Storrm
Storrm

Reputation: 102

As you already described a type of "model" you now have to implement it in a lower level.

First separate fields into groups of mandatory and additional fields. Since you want to send an email, at least the mail field is a mandatory one. The rest is up to you.

Afterwards you have to implement a logic into php that checks if your mandatory fields are not blank. If so, you can send the mail with the information you got (Maybe this makes another blank-check necessary). Otherwise you reject the request and print your message.

You can do it on client-side and server side. On client-side you have to add the HTML5 attribute required to the input-fields. But this one isn't something you can rely on. It just saves some requests. On the Server side you have to check all mandatory fields before sending the mail.

When you provide more information we can give you further hints how to implement the logic in php.

Upvotes: 0

Related Questions