I've been trouble-shooting an issue with a form with a lot of fields not populating correctly through a PHP mail form.
I went back to basics, broke it down to just a few fields and I seem to be having an issue when the second set of checkbox data is added into the mix. As it's copied below, the email will send and the body includes everything up to and including $sport_msg. It does NOT include $availability_msg or $message, though it was doing so before I added the second set of checkbox data.
I'm 90% sure the issue is with my syntax for $email_body, but I'm not sure what I've got wrong. Or it could have to do with the brackets around my 'foreach' pieces of the two sets of checkboxes. Can someone better at PHP than myself give me another set of eyes please?
My PHP:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$sport = $_POST['sport'];
$message = $_POST['message'];
foreach($_POST['availability'] as $value) {
$availability_msg .= "$value\n";
}
foreach($_POST['sport'] as $value) {
$sport_msg .= "$value\n";
}
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'VGFV Guide Signup';//<== Shows up in the "from" field
$email_subject = "New Guide Signup";//<== Email Subject
//Begin Email Body - I BELIEVE THIS IS WHERE MY ISSUE IS!!!
$email_body = "$name. has signed up from $company\n\n".
"Here is the info:\n \n".
"Guide Services Offered: \n".$sport_msg;
"Availability: \n".$availability_msg;
"$message \n \n".
$to = "myemailaddress@mydomain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
Aucun commentaire:
Enregistrer un commentaire