Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Sending form data while omitting blank fields

    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 Code:
    <?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>";
    }
    ?>
    Last edited by Inigoesdr; 01-26-2012 at 04:50 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by nobi View Post
    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.
    PHP Code:
    $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');
    //... 

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    . 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.

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

  • Users who have thanked Fou-Lu for this post:

    nobi (01-26-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    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?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    function showNonEmpty($var$label)
    {
        
    $sResult '';
        if (!empty(
    $var))
        {
            
    $sResult sprintf("%s: %s\n"$label$var);
        }
        return 
    $sResult;

    Instead of the ternary.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •