Results 1 to 4 of 4

Thread: [RESOLVED] PHP-Don't include blank form fields when emailed

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    Resolved [RESOLVED] PHP-Don't include blank form fields when emailed

    Hello, I have a simple HTML form asking for multiple user info. Most of the form fields are not required to be filled out. The form fields are then sent through PHP to then be emailed to me.

    My question: How do I not include the blank form fields when emailed?!
    My requirement: I want the resulting email to have a nice layout as you can see from here:
    Code:
    $email_message .= "Attendance: ".clean_string($attendance)."\n\n";
    	$email_message .= "First Name: ".clean_string($first_name)."\n";
    	$email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= ($first_name)."'s food restrictions: ".clean_string($other_allergies1)."\n\n";
    The result in my email:
    Attendance: Wedding_only

    First Name: Joe
    Last Name: Smith
    Joe's food restrictions: none
    Oh! And I'm a total noob. I know just enough to modify existing code and just enough to get myself into trouble. So if you do reply, please spell it out step-by-step or just show me my code=modified.
    Thank you for your time!


    My Code:
    Code:
    $attendance = $_POST['attendance']; // required
    	$first_name = $_POST['guest1-fn']; // required
    	$last_name = $_POST['guest1-ln']; // required
    	$other_allergies1 = $_POST['other_allergies1']; // not required
    	$guest2_fn = $_POST['guest2-fn']; // not required
    	$guest2_ln = $_POST['guest2-ln']; // not required
    	$other_allergies2 = $_POST['other_allergies2']; // not required
    	$guest3_fn = $_POST['guest3-fn']; // not required
    	$guest3_ln = $_POST['guest3-ln']; // not required
    	$other_allergies3 = $_POST['other_allergies3']; // not required
    	$guest4_fn = $_POST['guest4-fn']; // not required
    	$guest4_ln = $_POST['guest4-ln']; // not required
    	$other_allergies4 = $_POST['other_allergies4']; // not required
    	$guest5_fn = $_POST['guest5-fn']; // not required
    	$guest5_ln = $_POST['guest5-ln']; // not required
    	$other_allergies5 = $_POST['other_allergies5']; // not required
    	$guest6_fn = $_POST['guest6-fn']; // not required
    	$guest6_ln = $_POST['guest6-ln']; // not required
    	$other_allergies6 = $_POST['other_allergies6']; // not required
    	$hotel = $_POST['hotel']; // not required
    	$transportation = $_POST['transportation']; // not required
    	$rentalshare = $_POST['rentalshare']; // not required
    	$taxishare = $_POST['taxishare']; // not required
    	$tigers = $_POST['tigers']; // not required
    	$email_from = $_POST['email']; // not required
    	
    
    	$email_message .= ($first_name)."'s RSVP.\n\n";
    	
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	}
       
    	$email_message .= "Attendance: ".clean_string($attendance)."\n\n";
    	$email_message .= "First Name: ".clean_string($first_name)."\n";
    	$email_message .= "Last Name: ".clean_string($last_name)."\n";
    	$email_message .= ($first_name)."'s food restrictions: ".clean_string($other_allergies1)."\n\n";
    	$email_message .= "Guest 2 First Name: ".clean_string($guest2_fn)."\n";
    	$email_message .= "Guest 2 Last Name: ".clean_string($guest2_ln)."\n";
    	$email_message .= ($guest2_fn)."'s food restrictions: ".clean_string($other_allergies2)."\n\n";
    	$email_message .= "Guest 3 First Name: ".clean_string($guest3_fn)."\n";
    	$email_message .= "Guest 3 Last Name: ".clean_string($guest3_ln)."\n";
    	$email_message .= ($guest3_fn)."'s food restrictions: ".clean_string($other_allergies3)."\n\n";
    	$email_message .= "Guest 4 First Name: ".clean_string($guest4_fn)."\n";
    	$email_message .= "Guest 4 Last Name: ".clean_string($guest4_ln)."\n";
    	$email_message .= ($guest4_fn)."'s food restrictions: ".clean_string($other_allergies4)."\n\n";
    	$email_message .= "Guest 5 First Name: ".clean_string($guest5_fn)."\n";
    	$email_message .= "Guest 5 Last Name: ".clean_string($guest5_ln)."\n";
    	$email_message .= ($guest5_fn)."'s food restrictions: ".clean_string($other_allergies5)."\n\n";
    	$email_message .= "Guest 6 First Name: ".clean_string($guest6_fn)."\n";
    	$email_message .= "Guest 6 Last Name: ".clean_string($guest6_ln)."\n";
    	$email_message .= ($guest6_fn)."'s food restrictions: ".clean_string($other_allergies6)."\n\n";
    	$email_message .= "Hotel/Accomodations: ".clean_string($hotel)."\n\n";
    	$email_message .= "Transportation: ".clean_string($transportation)."\n";
    	$email_message .= "Rental Car: ".clean_string($rentalshare)."\n";
    	$email_message .= "Taxi: ".clean_string($taxishare)."\n\n";
    	$email_message .= "Tigers Game: ".clean_string($tigers)."\n\n";
    	$email_message .= "Email: ".clean_string($email_from)."\n";
    	
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
    Last edited by diedrichg; Apr 18th, 2010 at 11:49 PM. Reason: formatting

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP-Don't include blank form fields when emailed

    since you're not actually doing any validation (you simply create variables out of the $_POST globals, but do not actually make sure these have a value), we could simply loop through the $_POST array and only add onto $email_message when the value of that index is not empty. in the last piece of code you posted, this would replace all of the code you have from the beginning until the "create email headers comment":
    PHP Code:
    // start the message as an empty string
    $email_message "";

    // loop through the POST array getting the index ($key) and the value ($value):
    foreach($_POST as $key => $value){

      
    // is this value not empty?
      
    if(!empty($value)){

        
    // it is not empty. add onto the $email_message
        
    $email_message .= "{$key}: " clean_string($value) . "\r\n";

      }

    // end foreach 
    this will give you the basic functionality of what you want. however, we end up with a slight problem. this will produce the following results (as an example):
    Code:
    attendance: ...
    guest1-fn: ...
    guest1-ln: ...
    guest2-fn: ...
    guest2-fn: ...
    etc.
    this is usable (because you created this form and you understand it), but it may not be desirable. because we are using the index ($key) in our foreach loop, we are getting the name of the <input> fields on your form, not the labels (PHP has no idea what you labeled your fields, it only knows the name attribute as that is what gets sent to the server). we can make a work around for this by creating an associative array that will hold reference to all of the labels that we want. here is an example of the first few indexes ($keys) and how they would reference the label that would show up in the email:
    PHP Code:
      $labels = array(
                      
    'attendance' => 'Attendance',
                      
    'guest1-fn' => 'Guest 1 First Name',
                      
    'guest1-ln' => 'Guest 1 Last Name',
                      
    'other_allergies1' => 'Guest 1 Allergies',
                      ...
                      
    'guest6-fn' => 'Guest 6 First Name',
                      
    'guest6-ln' => 'Guest 6 Last Name',
                      
    'other_allergies6' => 'Guest 6 Allergies',
                      
                      
    'hotel' => 'Hotel',
                      
    'transportation' => 'Transportation',
                      ...
                      ); 
    I hope that format makes sense to you. as you can hopefully see, we are using the name of the field on the form as the index (or key) to the array, which is associated with the label for that particular index. I only filled in enough to hopefully give you the idea, however. then, we can modify the code from above to incorporate our new array, which should give us a final product that looks a little like this:
    PHP Code:
      // our array of form labels
      
    $labels = array(
                      
    'attendance' => 'Attendance',
                      
    'guest1-fn' => 'Guest 1 First Name',
                      
    'guest1-ln' => 'Guest 1 Last Name',
                      
    'other_allergies1' => 'Guest 1 Allergies',
                      ...
                      
    'guest6-fn' => 'Guest 6 First Name',
                      
    'guest6-ln' => 'Guest 6 Last Name',
                      
    'other_allergies6' => 'Guest 6 Allergies',
                      
                      
    'hotel' => 'Hotel',
                      
    'transportation' => 'Transportation',
                      ...
                      );

      
    // start the message as an empty string
      
    $email_message "";

      
    // loop through the POST array getting the index ($key) and the value ($value):
      
    foreach($_POST as $key => $value){

        
    // is this value not empty?
        
    if(!empty($value)){

          
    // it is not empty. add onto the $email_message
          
    $email_message .= (isset($labels[$key]) ? $labels[$key] : $key) . ": " clean_string($value) . "\r\n";

        }

      } 
    // end foreach 
    the new code in question is below, which is called a ternary operation (or a shorthand if statement):
    PHP Code:
    (isset($labels[$key]) ? $labels[$key] : $key
    that code basically acts the same as:
    PHP Code:
    if(isset($labels[$key])){
      
    $email_message .= $labels[$key];
    }else{
      
    $email_message .= $key;

    the ternary operation saves us space and time because it is inline, though it may not be easily understood at first glance. if the second example makes more sense to you, I implore you to use that instead! whichever you choose to use, this little if statement basically makes sure that a label is associated with it in the $labels array before adding it to the $email_message; if a label is not associated with it, then we fall back to using just the index ($key) instead to be able to show something to the person receiving the email.

    so. that's a fairly basic way of doing what you'd like to do. the next step would be to stop iteration from the processing and outputting parts of your form and combine them. basically, instead of creating your form statically, you would create an array that held all of your form elements' properties (what type of input field, the name, the label, etc), and you would look through that to both build and process your form. however, that would take a lot more work!

    anyhow, I hope this had made some sense to you. if not, feel free to ask more questions.
    Last edited by kows; Apr 19th, 2010 at 12:37 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    Re: PHP-Don't include blank form fields when emailed

    WOW! Thank you kows! What an amazing reply. Thanks so much! I tried your suggestion but I just simply wasn't able to get it to do what I wanted it to do. Issue 1 was that I was getting _all_ form fields sent to the email output including the captcha code and even the "submit" from the submit button. My other issue was that I just didn't have enough knowledge to add in line breaks within(or outside) the array. I wound up having a reply from another person on a different board. I implemented their suggestion and it worked perfectly. It was:

    if (isset($_POST['fieldname'])&&$_POST['fieldname']!='') { .... }

    I still have anal'istic issues with line breaks if a form field was not filled out but I made a dirty workaround with the

    $email_message .= "\n\n";

    as you can see. But I digress, that's my own mental issue.

    Again! Thank you so much for putting in all that time and effort into your suggestion and descriptions!

    Here is a sample of my original code:
    Code:
    	$attendance = $_POST['attendance']; // required
    	$first_name = $_POST['guest1-fn']; // required
    	$last_name = $_POST['guest1-ln']; // required
    	$other_allergies1 = $_POST['other_allergies1']; // not required
    	$guest2_fn = $_POST['guest2-fn']; // not required
    	$guest2_ln = $_POST['guest2-ln']; // not required
    	$other_allergies2 = $_POST['other_allergies2']; // not required
    
    	$email_message .= ($first_name)."'s RSVP.\n\n";
    	
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	}
       
    	$email_message .= "Attendance: ".clean_string($attendance)."\n\n";
    	$email_message .= "First Name: ".clean_string($first_name)."\n";
    	$email_message .= "Last Name: ".clean_string($last_name)."\n";
    	$email_message .= ($first_name)."'s food restrictions: ".clean_string($other_allergies1)."\n\n";
    	$email_message .= "Guest 2 First Name: ".clean_string($guest2_fn)."\n";
    	$email_message .= "Guest 2 Last Name: ".clean_string($guest2_ln)."\n";
    	$email_message .= ($guest2_fn)."'s food restrictions: ".clean_string($other_allergies2)."\n\n";
    The suggestion that worked:
    Code:
    	$attendance = $_POST['attendance']; // required
    	$first_name = $_POST['guest1_fn']; // required
    	$last_name = $_POST['guest1_ln']; // required
    	$other_allergies1 = $_POST['other_allergies1']; // not required
    	$guest2_fn = $_POST['guest2_fn']; // not required
    	$guest2_ln = $_POST['guest2_ln']; // not required
    	$other_allergies2 = $_POST['other_allergies2']; // not required
    
       	$email_message .= ($_POST['guest1_fn']) . "'s RSVP\n\n";
    	
      	$email_message .= "Attendance: ".($_POST['attendance'])."\n\n";
    	
    		$email_message .= "First Name: ".($_POST['guest1_fn'])."\n";
    		$email_message .= "Last Name: ".($_POST['guest1_ln'])."\n";
    	if(isset($_POST['other_allergies1'])&&$_POST['other_allergies1']!=''){
    		$email_message .= ($_POST['guest1_fn']) . "'s food restrictions: ".($_POST['other_allergies1']);}
    		$email_message .= "\n\n";
    		
    	if (isset($_POST['guest2_fn'])&&$_POST['guest2_fn']!=''){
    		$email_message .= "First Name: ".($_POST['guest2_fn'])."\n";}
    	if(isset($_POST['guest2_ln'])&&$_POST['guest2_ln']!=''){
    		$email_message .= "Last Name: ".($_POST['guest2_ln'])."\n";}
    	if(isset($_POST['other_allergies2'])&&$_POST['other_allergies2']!=''){
    		$email_message .= ($_POST['guest2_fn']) . "'s food restrictions: ".($_POST['other_allergies2']);}
    		$email_message .= "\n\n";
    Here is the output email:
    Joe's RSVP

    Attendance: Wedding_only

    First Name: Joe
    Last Name: Smith
    Joe's food restrictions: low sodium

    First Name: Sally
    Last Name: Smith


    First Name: John
    Last Name: Smith
    John's food restrictions: gluten







    Hotel/Accomodations: HolidayInn

    Transportation: rental


    Email: [email protected]

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] PHP-Don't include blank form fields when emailed

    it would be simple to change the $labels array into an array that decided exactly what showed up in your email (so something that did not have a label associated with it would not be shown in the email), which sounds more like what you would like then.

    if you would be interested in such a solution, feel free to ask for help. otherwise, it's good that you got it working anyway.

Posting Permissions

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



Click Here to Expand Forum to Full Width