Answer the question
In order to leave comments, you need to log in
How to send data from multiple fields?
There is a form with a button that adds another one of the same form to the page. All selects and inputs in these forms have the same names. As a result, only what is entered in the last form is sent to the mail.
<?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'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$deliveryTime = $_POST['deliveryTime'];
$quantity = $_POST['quantity'];
$firstDish = $_POST['firstDish'];
$secondDish = $_POST['secondDish'];
$thirdDish = $_POST['thirdDish'];
$fourthDish = $_POST['fourthDish'];
$fifthDish = $_POST['fifthDish'];
$quantityBread = $_POST['quantityBread'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Поля Имя и email обязательны!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Неправильно введен адрес электронной почты!";
exit;
}
$email_from = '[email protected]';
$email_subject = "Новое сообщение";
$email_body = "У вас новое сообщение от $name.\n".
"Кол-во: $quantity \n".
"Первое блюдо: $firstDish \n".
"Второе блюдо: $secondDish \n".
"Гарнир: $thirdDish \n".
"Салат: $fourthDish \n".
"Хлеб: $fifthDish \n".
"Кол-во хлеба: $quantityBread \n".
"Телефон: $phone \n".
"Адрес доставки: $address \n".
"Желаемое время доставки: $deliveryTime \n".
"Комментарий: $message \n";
$to = "[email protected], [email protected]";
$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 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;
}
}
?>
Answer the question
In order to leave comments, you need to log in
$_POST['submit'] is the name of the button, the name attribute. Just give the second form a name like submit_second
if(isset($_POST['submit']))
{
//Your code for the first form
}else if (isset($_POST['submit_second']))
{
//Your code for second form
}
And so I would check the fields in JS and send Ajax
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question