I have a PHP email form, working very nicely in a site. I was asked to develop an extensive multi-choice form on the same model for a client. The form works great but there are a lot of fields. When a user doesn’t use some of the fields, is there any way the empty fields can be suppressed from the email message and only contains the required fields and the non-required that are filled?
The client otherwise receives a large email with empty fields which is cumbersome. I have been digging the internet for answers but haven’t anything that either worked or was clear to implement. Please see below a basic PHP email form sample. I appreciate any input. Thanks! Eric
<?php /* Set e-mail recipient */ $myemail = "[email protected]"; $subject = "Submission"; $message = "Hello"; $headers = "From: [email protected]" . "\r\n" . /* Check all form inputs using check_input function */ $firstname = check_input($_POST['firstname']); $lastname = check_input($_POST['lastname']); $company = check_input($_POST['company']); $title = check_input($_POST['title']); $industry = check_input($_POST['industry']); $address = check_input($_POST['address']); $city = check_input($_POST['city']); $state = check_input($_POST['state']); $zipcode = check_input($_POST['zipcode']); $phonenumberA = check_input($_POST['phonenumberA']); $phonenumberB = check_input($_POST['phonenumberB']); $phonenumberC = check_input($_POST['phonenumberC']); $cellularnumberA = check_input($_POST['cellularnumberA']); $cellularnumberB = check_input($_POST['cellularnumberB']); $cellularnumberC = check_input($_POST['cellularnumberC']); $faxnumberA = check_input($_POST['faxnumberA']); $faxnumberB = check_input($_POST['faxnumberB']); $faxnumberC = check_input($_POST['faxnumberC']); $email = check_input($_POST['email']); $website = check_input($_POST['website']); $comments = check_input($_POST['comments']); $receiveemail = check_input($_POST['receiveemail']); /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } /* If URL is not valid set $website to empty */ if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) { $website = ''; } /* Let's prepare the message for the e-mail */ $message = "Hello! Your contact form has been submitted by: First Name*: $firstname Last Name*: $lastname Company*: $company Title: $title Industry: $industry Address: $address City: $city State: $state Zip Code: $zipcode Telephone*: $phonenumberA Telephone*: $phonenumberB Telephone*: $phonenumberC Cellular: $cellularnumberA Cellular: $cellularnumberB Cellular: $cellularnumberC Fax: $faxnumberA Fax: $faxnumberB Fax: $faxnumberC Email Address*: $email Website URL: $website Comments: $comments Yes, I would like to receive email messages from WPL : $receiveemail End of message "; /* Send the message using mail() function */ mail($myemail, $subject, $message, $headers); $user = "$email"; $usersubject = "Thank You"; $userheaders = "From: [email protected]\n"; $usermessage = "Thank you for contacting us. We will respond to your inquiry as soon as possible. WPL 4 John Walsh Boulevard Peekskill, NY 10566 1.800.825.4646"; mail($to,$subject,$message,$headers); mail($user,$usersubject,$usermessage,$userheaders); /* Redirect visitor to the thank you page */ header('Location: thankyou.html'); exit(); /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?><html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>