Jump to content

Sending form data while omitting blank fields


nobi

Recommended Posts

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>";
}
?>

Link to comment
Share on other sites

First off you should trim() the post data. THEN you need to determine which POST values are required to have data and test those before you generate the message variable

 

$to          = trim($_POST['to']);
$from        = trim($_POST['from']);
$name        = trim($_POST['name']);
$company     = trim($_POST['company'];
$newcustomer = trim($_POST['newcustomer']);
$address1    = trim($_POST['address1']);
$address2    = trim($_POST['address2']);
$subject     = ("Event Rental for " . $name . ""));
$description = trim($_POST['description']);
$phone       = trim($_POST['phone']);

//Check required fields
if(empty($to) || empty($from) || empty($name) || empty($company))
{
    //Required fields have no values
    //Provide error condition and don't generate email
}
else
{
    //Required fields have values
    //Generate email
}

Link to comment
Share on other sites

This is less about checking for required fields, and more about having an email message without a ton of blank fields, like Phone number: (blank) etc... though I definitely will consider tightening up on the requirements on these forms and your code looks good for that.

 

I plan to adapt this form to a much more complicated one I have involving requesting quotes for a bunch of different products, while still using a singular script for the different forms. Thats why I want to figure out how to leave off blank fields, because for example, on my "t-shirt" quote request page, I wont need a part of the message that says "Postcard size: (blank)"... Don't know if that makes any sense but thanks for taking a look at the script for me.

Link to comment
Share on other sites

OK, then you simply need to check each field before appending the relevant text to the message. You would still want to trim() the values first though. You could create a bunch of inline statements such as

$message = '';
if(!empty($name))
{
    $message .= "Name: {$name}\n";
}
if(!empty($company))
{
    $message .= "Company or Organization:{$company}\n";
}

 

Or you could create a function that you pass the variable and the full line to:

function addLine($var, $line)
{
    return (!empty($var)) ? "{$line}\n" : '';
}

$message = '';
$message .= addLine($name, "Name: {$name}");
$message .= addLine($company, "Company or Organization:{$company}");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.