I have this simple form to email in php. The nice thing about this form in the send php code is that I do NOT have to write out all the fields name.For example, I do NOT have to write this:
$msg .= "$_POST[ContactName] Contact Name: ContactName\n"
In my code, all I have to write is:
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
The form is currently working perfectly. It is currently sending this:
ContactName: name here
BusinessName: business name here
EmailAddress: email@email.com
Email_Confirmation:
PhoneNumber: 714-555-5555
However, I wish this to happen: If a field is left empty or the web user does not fill it a certain field box, then the form should NOT send this field. For example: web user decides to NOT fill in ContactName and or BusinessName. So the form should only send this format:
EmailAddress: email@email.com
Email_Confirmation:
PhoneNumber: 714-555-5555
Noticed no mention of ContactName: and BusinessName:
Please help! I would appreciate it. -Michelle Ha.
Here is the php send code:
if the Email_Confirmation field is empty
if(isset($_POST['Email_Confirmation']) && $_POST['Email_Confirmation'] == ''){
// put your email address here
$youremail = 'bomandty@gmail.com';
// prepare a "pretty" version of the message
$body .= "Thank you for your request. We will get back with you soon.";
$body .= "\n";
$body .= "\n";
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
$body .= "\n";
}
$CCUser = $_POST['EmailAddress'];
// Use the submitters email if they supplied one
// (and it isn't trying to hack your form).
// Otherwise send from your email address.
if( $_POST['EmailAddress'] && !preg_match( "/[\r\n]/", $_POST['EmailAddress']) ) {
$headers = "From: $_POST[EmailAddress]";
} else {
$headers = "From: $youremail";
}
// finally, send the message
mail($youremail, 'subject line here', $body, $headers, $CCUser );
}
// otherwise, let the spammer think that they got their message through
Add check to you foreach
And change default falue in your form to placeholders