View Full Version : Emailing only the filled-in fields...
sakka
04-16-2008, 05:42 PM
Hi everyone,
I'm stumped on how to start this code. I have a basic email script written up, but not sure how to modify it so it has the following functionality.
I have a giant form, and really only need the fields that are filled in to be emailed when the user clicks "Submit". I am guessing it all goes into an array , but how do I auto-generate a table from the information in the email?
Any thoughts or help would be greatly appreciated.. thank you :o
Sakka
mlseim
04-16-2008, 06:10 PM
Maybe you can post your script?
The form data will end up in the $_POST (or possibly $_GET, though it probably shouldn't...) superglobal array. You can check whether a variable contains an 'empty' value with the empty() function (isset() won't work, as text-fields in forms will submit '' when blank - which I'd assume you'd count as not filled-in), so something along the lines:
$email_body = '';
if(!empty($_POST['firstname)) {
$email_body .= 'Firstname: ' . $_POST['firstname'] . "\n;
}
if(!empty($_POST['surname'])) {
$email_body .= 'Surname: ' . $_POST['surname'] . "\n";
}
sakka
04-18-2008, 09:24 PM
Here is my email code. It's very basic, nothing fancy.
<?php
if (isset($_POST['submit']) && $_POST['submit'] == "Submit")
{
$rg_name = false;
$rg_phone = false;
$rg_emailv = false;
//Check If Empty Form
//Name
if ( empty( $_POST['name'] ) ) {
echo '<b>ERROR: Please enter a name.<br></b>'; }
else {
$rg_name = true; }
//Phone
if ( empty( $_POST['phone'] ) ) {
echo '<b>ERROR: Please enter a phone number.<br></b>'; }
else {
$rg_phone = true; }
//Email
if ( empty( $_POST['email'] ) ) {
echo '<b>ERROR: Please enter an email address.<br></b>'; }
else {
//Valid Email
if(!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $_POST['email'])) {
echo '<font color="red">ERROR: Please enter a valid Email<br>'; }
else {
$rg_emailv = $_POST['email']; }
}
// If - 2
if ($rg_name && $rg_phone && $rg_emailv) {
/////////////////////////SEND THE EMAIL //////////////////////////////
//To and From
$to = "delivery email address";
$from = $rg_emailv;
$Headers =<<<EH
To: $to
From: $from
X-Mailer: PHP/4.4.2
MIME-Version: 1.0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: base64
Reply-to: $from
EH;
$Subject = "Subject of Email";
$MessageBody = chunk_split(base64_encode('
<html>
<body>
HTML email goes here ... where I need to put a table of the field in fields only.
</body>
</html>
'));
mail($to,$Subject,$MessageBody,$Headers,"-f $from");
//Thank you message after submit
echo "<br /><b>Thank you, " . $_POST['name'] . ". Your request has been submitted.</b><br /><br />";
}
}
?>
Would there be a way to loop through the array, get the variables that have filled in fields, and spit those out somehow? I would check with the empty() function, but this is a HUGE form - I'm talking about maybe 500+ fields. :( So that would be pretty intense to check if empty for each single variable.
whizard
04-18-2008, 09:30 PM
Maybe use a foreach loop to test each member of the $_POST superglobal array:
foreach($_POST as $value)
{
if(empty($value))
{
//Don't add it to the email
}
else
{
//Add it to the email
}
}
HTH,
Dan
sakka
04-23-2008, 07:41 PM
Hi Dan,
Thanks! I'll give it a shot and let you know how it turns out. That sounds like a possible solution! :thumbsup:
sakka
04-28-2008, 11:35 PM
Maybe use a foreach loop to test each member of the $_POST superglobal array:
foreach($_POST as $value)
{
if(empty($value))
{
//Don't add it to the email
}
else
{
//Add it to the email
}
}
HTH,
Dan
Hi Dan,
The problem I am running into is that there is no way to "add it to the email". All the contents of the email go into 1 variable ($MessageBody) at one time. I can't seem to add chunks of information afterwards to the message body. . . any clue?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.