Hello Fellow Stackers,
New to PHP and I am putting together a multipage form from pre-built code.
Basically the user selects as many checkboxes as they want… then the form submits to this secondary page. This secondary page echo’s the checkboxes they chose at the top of the page via $check.. then they can enter their contact information and all of the information gets submitted via form, along with the $check information.
Everything is working perfectly except $check isn’t being entered into the form message, but it works up at the top of the page, displaying which options the user inputted.
Any help is appreciated!
<?php
$emailOut = '';
if(!empty($_POST['choices'])) {
foreach($_POST['choices'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
$emailOut .= $check."\n"; //any output you want
}
}
$errors = '';
$myemail = 'test@myemailHERE.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. $check ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message \n $emailOut";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
The thing in this situation is that when you get down to the email,
$checkis the last option displayed. You need to use theforeachstatment to build the array or email output such asThen use your email variable in the same way
UPDATE
From further investigation and more code submitted, it appears that you are working with a multi-form submssion issue. The issue is you have form 1 (checkboxes) that submits to form 2 (email).
Since when doing the checks after the checkbox submission, no name, email, etc was given so
$errorswere given and no email sent. When filling out the email form, the checkboxes were not sent again so$checkor even$_POST['choices']had values.You can either put the two forms into one, or you can look into a way to save the values by passing them and filling a ‘hidden’ field (
<input type='hidden' value='...'>)or use a session with PHP.