Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Junior Member Jimmy Jones's Avatar
    Join Date
    Oct 2012
    Location
    Waxahachie, Texas
    Posts
    20
    Member #
    33137
    Liked
    2 times
    I'm using the following php script for a form mailer on a couple of sites. It works great and puts every field as a new line, but when I have optional fields on the form left empty, there is still a space on the email where the info would have been. Is there a way to strip out the blank space left by the empty form fields?

    I'm very new to php and I'm just beginning to understand how each part of this script works. If this script sucks, let me know!

    PHP Code:
    <?php

    if (empty($_POST)) {
        print 
    "<p>No data was submitted.</p>";
        print 
    "</body></html>";
        exit();
    }

    //Creates function that removes magic escaping, if it's been applied, from values and then removes extra newlines and returns to foil spammers. Thanks Larry Ullman!
    function clear_user_input($value) {
        if (
    get_magic_quotes_gpc()) $value=stripslashes($value);
        
    $valuestr_replace"\n"''trim($value));
        
    $valuestr_replace"\r"''$value);
        return 
    $value;
        }
     
    if (
    $_POST['comments'] == 'Please share any comments you have here'$_POST['comments'] = '';
     
    $body ="Here is the data that was submitted:\n";

    foreach (
    $_POST as $key => $value) {
        
    $key clear_user_input($key);
        
    $value clear_user_input($value);
        if (
    $key=='extras') {

        if (
    is_array($_POST['extras']) ){
            
    $body .= "$key: ";
            
    $counter =1;
            foreach (
    $_POST['extras'] as $value) {

                    if (
    sizeof($_POST['extras']) == $counter) {
                        
    $body .= "$value\n";
                        break;}
                    else {
                        
    $body .= "$value, ";
                        
    $counter += 1;
                        }
                    }
            } else {
            
    $body .= "$key$value\n";
            }
        } else {

        
    $body .= "$key$value\n";
        }
    }

    extract($_POST);

    $email clear_user_input($email);
    $name clear_user_input($name);
     
    $from='From: '$email "(" $name ")" "\r\n" 'Bcc: yourmail@yourdomain.com' "\r\n";
      
    $subject 'Bed Order from Web Site';
     
    mail ('youremail@yourdomain.com'$subject$body$from);
     
    ?>

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Show us the actual HTML form.


  4. #3
    Junior Member Jimmy Jones's Avatar
    Join Date
    Oct 2012
    Location
    Waxahachie, Texas
    Posts
    20
    Member #
    33137
    Liked
    2 times
    Here's one of the forms I'm using that could have blank info. It's not required that all fields are filled out, so if someone doesn't put info in a few of the fields in a row, I get the blank lines in the email.
    HTML Code:
    <form id="form3" method="post" action="warranty.php">
    
        <label for="First_Name" class="smalltext">First Name</label><br>
        <input type="text" name="fname" id="First_Name" size="20" /><br>
    
        <label for="Last_Name" class="smalltext">Last Name</label><br>
        <input type="text" name="lname" id="Last_Name" size="20" /><br>
    
        <label for="Email" class="smalltext">Email</label><br>
        <input type="text" name="email" id="Email" size="20" /><br>
    
        <label for="Phone" class="smalltext">Phone</label><br>
        <input type="text" name="phone" id="Phone" size="20" /><br>
    
        <label for="Todays_Date" class="smalltext">Today's Date</label><br>
        <input type="text" name="Todays_Date" id="Todays_Date" size="20" /><br>
    
        <label for="Closing_Date" class="smalltext">Closing Date</label><br>
        <input type="text" name="Closing_Date" id="Closing_Date" size="20" /><br>
    
        <label for="Address" class="smalltext">Address</label><br>
        <input type="text" name="Address" id="Address" size="50" /><br>
    
        <label for="Best_Time_To_Call" class="smalltext">Best Time To Call</label><br>
        <input type="text" name="Best_Time_To_Call" id="Best_Time_To_Call" size="20" /><br>
    
        <label for="Preferred_Method_Of_Contact" class="smalltext">Preferred Method Of Contact</label><br>
        <input type="text" name="Preferred_Method_Of_Contact" id="Preferred_Method_Of_Contact" size="20" /><br>
    
        <label for="Requested_Service" class="smalltext" id="Requested_Service">Requested Service(s)</label><br>
        <textarea name="Requested_Service" cols="50" rows="10"></textarea><br>
    
        <input name="Submit" id="submit1" type="submit" value="Submit" />
        <input type="reset" name="Clear_Form" id="Clear_Form" value="Clear Form" />
    
    </form>

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    To fix it for your script ... add the part shown in red ...

    foreach ($_POST as $key => $value) {
    $key = clear_user_input($key);
    $value = clear_user_input($value);
    if ($key=='extras') {
    if (is_array($_POST['extras']) ){
    $body .= "$key: ";
    $counter =1;
    foreach ($_POST['extras'] as $value) {
    if (sizeof($_POST['extras']) == $counter) {
    $body .= "$value\n";
    break;}
    else {
    $body .= "$value\n";
    $counter += 1;
    }
    }
    } else {
    $body .= "$key: $value\n";
    }
    } else {
    if($value){
    $body .= "$key: $value\n";
    }
    }
    }

    But if you want a formatted (pretty) email, you will have to do your email script a bit differently.
    It is what it is.


  6. #5
    Junior Member Jimmy Jones's Avatar
    Join Date
    Oct 2012
    Location
    Waxahachie, Texas
    Posts
    20
    Member #
    33137
    Liked
    2 times
    thanks!

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Waxahachie .... I had to look that one up...
    http://en.wikipedia.org/wiki/Waxahachie,_Texas

    Trying to remember where I saw that name before ...
    Then I remembered when I read wiki - the Super Collider (not)


  8. #7
    Junior Member Jimmy Jones's Avatar
    Join Date
    Oct 2012
    Location
    Waxahachie, Texas
    Posts
    20
    Member #
    33137
    Liked
    2 times
    Yep..the super collider that never was.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:37 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com