...

View Full Version : Sending form data while omitting blank fields



nobi
01-26-2012, 04:34 PM
Hi guys! New to the community and hope to learn a lot here!

Here is some background info and what I want to do, and what I know.

I have been building websites for years, and I am familiar with Actionscript 3.0. And I have successful ran a few wordpress websites. What I am trying to do right now however is modify this form script I found, to only email the data when the field isn't left blank. It's stumping me because the actual "message" that ends up being the body of the email is a variable. And form what I can tell I can't figure out how to put if statements into it. If anyone can take a look at this script and give me any pointers you would be awesome.


<?php
//trying to store the date in a separate variable only when it's not blank
if ($_POST['starttime'] == '') {
//nothing;
}
else
{
$showStartTime == "Start time: " . $_POST['starttime'] . "";
}
if ($_POST['finishtime'] == '')
{
//nothing;
}
else
{
$showFinishTime == "Finish time: " . $_POST['finishtime'] . "";
}
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$name = $_POST['name'];
$company = $_POST['company'];
$newcustomer = $_POST['newcustomer'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$subject = ("Event Rental for " . $name . "");
$description = $_POST['description'];
$phone = $_POST['phone'];

$message = ("
Name: " . $name . "
Company or Organization: " . $company . "
Phone Number: " . $phone . "
Email Address: " . $from . "
Street Address: " . $address1 . " " . $address2 . "
New Customer: " . $newcustomer . "
Customer From: " . $_POST['howyouheard'] . "
Interested in: SkyLoft " . $_POST['whichspace'] . "
Date: " . $_POST['date'] . "
Day of Week: " . $_POST['dayofweek'] . "
" . $showStartTime . "
" . $showFinishTime . "
Number of Guests: " . $_POST['Guests'] . "
Format: " . $_POST['format'] . "
Occasion: " . $_POST['Occasion'] . "
Optional Needs: " . $_POST['dj'] . " " . $_POST['tables'] . " " . $_POST['chairs'] . " " . $_POST['eventbanner'] . " " . $_POST['pasoundsystem'] . "
Message:
" . $description . "

");

// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p><b>Thank you for your interest in SkyLofts!</b> <br>You should recieve a verification in your inbox that we recieved your request. We will contact you as soon as possible and hopefully be able to answer any questions you may have! You can also contact us via phone 410-791-6699, or toll-free 1-800-344-0410.</p>";
} else {
echo "<p>There was an error when processing your request. Please try again.</p>";
}
?>

Fou-Lu
01-26-2012, 05:21 PM
It's stumping me because the actual "message" that ends up being the body of the email is a variable.

Can you expand on this please? What does this show in the actual resulting body? Looks to me like the parsing is occurring here, so can you clarify what you mean by it showing as a variable?

Its a simple matter of building your string with a series of checks. I'd do them one at a time since its a little easier to read and you never need to worry about additional whitespace.


$message = '';
$message .= !empty($name) ? 'Name: ' . $name . "\n" : '';
$message .= !empty($company) ? 'Company : ' . $company . "\n" : '';
//...
// Another option is a simple function:
function showNonEmpty($var, $label)
{
return !empty($var) ? sprintf("%s: %s\n", $label, $var) : '';
}
$message = '';
$message .= showNonEmpty($name, 'Name');
$message .= showNonEmpty($company, 'Company');
//...

nobi
01-26-2012, 05:58 PM
so if I'm understanding.... $message .= adds on to the variable with whatever follows the ".=" ?

If you notice in the script I have already

$message ends up equaling the entire message body, thats what I meant by it being a variable.

The if else I have at the top is my failed attempt at accomplishing what I need. I guess I'll try out the code you suggested.

This is going to be used for a much bigger application I just want to figure out the best code for this first.

Fou-Lu
01-26-2012, 06:05 PM
. is a concatenation operation in PHP, just as you used within the original $message. The only difference with mine is that it checks each entry and then concats to the existing variable, while the original $message is a single string of concatenation.
My concern was that you actually receive an email with the phrase $message in it. If that's not the case and it is showing the parsed message as you have, then that is fine.


BTW, the if[/else] is exactly what you need. I used a ternary operation in mine with direct concat instead of an if check.

nobi
01-26-2012, 06:21 PM
BTW, the if[/else] is exactly what you need. I used a ternary operation in mine with direct concat instead of an if check.


Thanks for the help, your code seems to work. I have no absolutely no training in PHP so that .= snippet is going to help me a lot.

Do you suggest I use your version of the code with the "ternary" functions or write a traditional if else?

Fou-Lu
01-26-2012, 06:34 PM
Ternary is shorter, if/else more readable. In either case I'd make use of the function since it will cut down on your source code. Using if statement in the function would be a simple:


function showNonEmpty($var, $label)
{
$sResult = '';
if (!empty($var))
{
$sResult = sprintf("%s: %s\n", $label, $var);
}
return $sResult;
}

Instead of the ternary.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum