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 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    14
    Thanked 1 Time in 1 Post

    problem of showing only the filled in results sent to email

    hey people, is there any way of sending only the filled in details to email?

    i will have roughly 60 input fields in a order form, however all 60 input fields are not mandatory.

    the coding so far is:

    PHP Code:

    <?php 
    if (isset($_REQUEST['email'])) 
    //if "email" is filled out, send email 

      



      
    //send email 
      
    $to "razzyc86@hotmail.co.uk";
      
    $subject "order form submitted";
     
    $email $_REQUEST['email'] ; 

      
    // To allow HTML codes to email!
      
    $headers  'MIME-Version: 1.0' "\r\n";
      
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
      
    $headers .= "From: $email\r\n"


      
    $message "
      <html>
      <head><title>Order placed</title></head>
      <body>
     <p>Email is from  "
    .$email."</p>
      <p>First name: "
    .$_POST["first_name"]."<br />
            Last name: "
    .$_POST["last_name"]."<br />
            Address: "
    .$_POST["address"]."<br/>
            Postcode: "
    .$_POST["postcode"]."<br />
            Tel no: "
    .$_POST["tel_no"]."<br />
    </p>
       </body>
       </html>"
    ;

      
    mail($to,$subject,$message,$headers);

      echo 
    "Thank you for using our mail form"
      } 

    ?>
    so basically so far i have 5 fields:

    first name: xxxxxxx
    last name: xxxxxxxxxx
    address: xxxxxxxxxxxxxxx
    postcode:xxxxxxxx
    tel no: xxxxxxxxxxxx

    customer does not need to enter the postcode, so if they leave that blank then i dnt want the postcode part to show up on the email body, for example:

    first name: xxxxxxx
    last name: xxxxxxxxxx
    address: xxxxxxxxxxxxxxx
    tel no: xxxxxxxxxxxx

    i hope i dont need to make drastic changes to the coding so far as some of the guys here have helped me alot to create the php email coding

    thank you very much

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    PHP Code:

      $message 

      <html> 
      <head><title>Order placed</title></head> 
      <body> 
     <p>Email is from  "
    .$email."</p> 
      <p>First name: "
    .$_POST["first_name"]."<br /> 
            Last name: "
    .$_POST["last_name"]."<br /> 
            Address: "
    .$_POST["address"]."<br/>";

    if(
    $_POST['postcode']){
    $message .= "
    Postcode: "
    .$_POST["postcode"]."<br />";
    }

    $message .= "
    Tel no: "
    .$_POST["tel_no"]."<br /> 
    </p> 
       </body> 
       </html>"


  • #3
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by mlseim View Post
    PHP Code:

      $message 

      <html> 
      <head><title>Order placed</title></head> 
      <body> 
     <p>Email is from  "
    .$email."</p> 
      <p>First name: "
    .$_POST["first_name"]."<br /> 
            Last name: "
    .$_POST["last_name"]."<br /> 
            Address: "
    .$_POST["address"]."<br/>";

    if(
    $_POST['postcode']){
    $message .= "
    Postcode: "
    .$_POST["postcode"]."<br />";
    }

    $message .= "
    Tel no: "
    .$_POST["tel_no"]."<br /> 
    </p> 
       </body> 
       </html>"

    Shouldn't it be:
    PHP Code:
    if( empty($_POST['postcode']) ){ 
    Or it doesn't matter?

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Here is my attempt but I've not had a chance to test it.

    PHP Code:
     $message 
      <html> 
      <head><title>Order placed</title></head> 
      <body> 
     <p>Email is from  "
    .$email."</p> 
      <p>"
    ;

    $details = array(array("First name" => "first_name"), /* repeat.. */ );
    $trimmed array_map('trim'$_POST);
    $cleaned array_map('htmlentities'$trimmed);

    foreach (
    $details as $fields) {
        foreach (
    $fields as $title => $fieldname) {
            if (isset(
    $cleaned["'$fieldname'"]) && !empty($cleaned["'$fieldname'"])) {
                
    $message .= "$title: {$cleaned['$fieldname']}</br>";
            }
        }
    }

    $message .= "</p>
       </body> 
       </html>"

    Not sure at the moment if this: isset($cleaned["'$fieldname'"]) - is correct(?).

    It seems to be a common omission for people not to clean post data before re-inserting it into html.

    @ECoode I prefer to explicitly use isset() and empty() but I believe 'if($_POST['postcode'])' is equivalent.
    Last edited by AndrewGSW; 08-03-2012 at 07:15 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    New Coder
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    14
    Thanked 1 Time in 1 Post
    thanx guys, however i only used the postcode field as an example, majority of the 60+ fields will not be mandatory, does that mean im gonna have to use the....if($_POST[...code on each of the non mandatory fields? or is there a simpler way of using one code for all the fields>

    thanx

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Following my previous post, if the fields are consistently named like this: 'first_name', 'address' you could loop through them converting them into a new array with 'First name', 'first_name', 'Address', 'address', etc.

    Using this approach you wouldn't have to type all 60 title/field name combinations. You would just need to exclude those fields that don't need to be checked, and probably manually insert 'Post Code', 'postcode'.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Something like this:

    PHP Code:
    <?php
     $message 

      <html> 
      <head><title>Order placed</title></head> 
      <body> 
     <p>Email is from  "
    .$email."</p> 
      <p>"
    ;

    // $details = array(array("First name" => "first_name"), /* repeat.. */ );
    $trimmed array_map('trim'$_POST);
    $cleaned array_map('htmlentities'$trimmed);

    $extracted = array();
    $exclude = array("field_name1""fieldname2");      // the fields you don't need to check

    foreach ($cleaned as $key => $value) {
        if (!
    in_array($key$exclude)) {
            
    $extracted[] = array(ucwords(str_replace("_"" "$key)), $value);
        } 
    }

    foreach (
    $extracted as $fields) {
        if (!empty(
    $fields[1])) {
            
    $message .= "$fields[0]: $fields[1]</br>";
        }
    }

    $message .= "</p>
       </body> 
       </html>"
    ;

    ?>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If you want to do it in one loop..

    PHP Code:
    <?php
     $message 

      <html> 
      <head><title>Order placed</title></head> 
      <body> 
     <p>Email is from  "
    .$email."</p> 
      <p>"
    ;

    $trimmed array_map('trim'$_POST);
    $cleaned array_map('htmlentities'$trimmed);

    $exclude = array("field_name1""fieldname2");      // the fields you don't need to check

    foreach ($cleaned as $key => $value) {
        if (!
    in_array($key$exclude) && !empty($value)) {
            
    $message .= ucwords(str_replace("_"" "$key)) . ": $value</br>";
        } 
    }

    $message .= "</p>
       </body> 
       </html>"
    ;

    ?>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    Razzy (08-03-2012)

  • #9
    New Coder
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    14
    Thanked 1 Time in 1 Post
    ahh thank you andrewGSW, i decided to do it in one loop and its perfect

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by Razzy View Post
    ahh thank you andrewGSW, i decided to do it in one loop and its perfect
    Cool, no problem. Thank you for testing it for me
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #11
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,999
    Thanks
    15
    Thanked 234 Times in 234 Posts
    So you have five or so required fields, you can give them all individual names. But of the fifty five or so left, what kind of data will they have? Are they just a list of different things? You could give them all the same name like this name="order[]", and when you upload they would be an array, you could just loop over the elements to get the value, put an if statement in to remove any empty elements, and that would do it. You could even have javascript make as many inputs as you needed.

  • #12
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by DrDOS View Post
    So you have five or so required fields, you can give them all individual names. But of the fifty five or so left, what kind of data will they have? Are they just a list of different things? You could give them all the same name like this name="order[]", and when you upload they would be an array, you could just loop over the elements to get the value, put an if statement in to remove any empty elements, and that would do it. You could even have javascript make as many inputs as you needed.
    The inputs need to keep their individual names as these are converted to headings within the email.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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