Results 1 to 7 of 7

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

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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; 04-20-2010 at 01:07 AM. Reason: formatting

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

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

    "If 'fieldname' was submitted and has a value other than blank, ...."
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    hmmmm.. I think you can just do this:
    1.) initialize $email_message to nothing ( $email_message = "" )
    2.) check if $_POST['index'] is available ( if(isset($_POST['guest1-fn'])) )
    3.) if yes, set your message
    ( $email_message .= ($_POST['guest1-fn']) . "'s RSVP.\n\n"; )


    Code:
    $email_message = "";
    
    if(isset($_POST['guest1-fn']))
       $email_message .= ($_POST['guest1-fn']) . "'s RSVP.\n\n";
    do this for the rest of entries.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The problem with that is that sometimes, depending on the type of form field [textarea, for example, I think], browser and possibly other factors [server?], a field will be SET, but empty. And other fields will not be set at all. So to avoid a warning about referring to an index that doesn't exist you must use isset() and to be sure you're not inserting a blank value you must use $x['x']!=''.

    It's possible to get around that in some cases, but I haven't found it to be consistent enough to be reliable.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thank you djr33! Your code worked perfectly. Thanks so much! For the fun of it I tried heavensgate's code and it did exactly what you said it would do, it returned all fields whether they were filled in or not.

    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";
    Your 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: me@you.com

  6. #6
    Join Date
    Mar 2008
    Posts
    122
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Default

    You can use this following piece of code to return a default value if one of the fields were left empty:

    PHP Code:

    $attendance 
    = !empty($_POST['attendance']) ? clean_string($_POST['attendance']) : "value to return if \$_post['attendance'] is empty"
    Heres abit of explaining:
    PHP Code:

    $variable_name 
    statement if_statement_is_true if_statement_is_false
    In your case, if $_POST['attendance'] is not empty, it will be returned with clean_string, if it is empty it wil return, for ex, "value to return if \$_post['attendance'] is empty".

    Basically it's a shorthand if...else statement.

    Usefull Links:
    http://php.codenewbie.com/articles/p...or-Page_1.html


    Hope i helped
    Last edited by hmsnacker123; 04-20-2010 at 01:39 AM.

  7. #7
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    ahihihihihihi... yeah, I forgot that one hehe...

Bookmarks

Posting Permissions

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