Log in

View Full Version : Resolved PHP-Don't include blank form fields when emailed



diedrichg
04-19-2010, 04:38 AM
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:

$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:

$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);

djr33
04-19-2010, 04:59 AM
if (isset($_POST['fieldname'])&&$_POST['fieldname']!='') { .... }

"If 'fieldname' was submitted and has a value other than blank, ...."

heavensgate15
04-19-2010, 10:59 AM
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"; )





$email_message = "";

if(isset($_POST['guest1-fn']))
$email_message .= ($_POST['guest1-fn']) . "'s RSVP.\n\n";



do this for the rest of entries.

djr33
04-19-2010, 05:07 PM
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.

diedrichg
04-20-2010, 01:06 AM
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:

$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:

$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

hmsnacker123
04-20-2010, 01:33 AM
You can use this following piece of code to return a default value if one of the fields were left empty:




$attendance = !empty($_POST['attendance']) ? clean_string($_POST['attendance']) : "value to return if \$_post['attendance'] is empty";



Heres abit of explaining:



$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/php/1480/The_Ternary_Operator-Page_1.html


Hope i helped

heavensgate15
04-20-2010, 01:57 AM
ahihihihihihi... yeah, I forgot that one hehe... :D