HTML:
<div class="form">
<div class="input input-name">
<h4 class="form-title">What is your name <span>*</span></h4>
<div class="first-name">
<input class="input-name-1" type="text">
<p>First Name</p>
</div>
<div class="last-name">
<input class="input-name-2" type="text" >
<p>Last Name</p>
</div>
</div>
<div class="input input-phone">
<h4 class="form-title">Phone Number <span>*</span></h4>
<input class="input-phone" type="tel" >
</div>
<div class="input input-address">
<h4 class="form-title">Your address <span>*</span></h4>
<input class="input-address" type="text">
</div>
<button class="button" id="button-reg">order</button>
<div class="message"></div>
</div>
JS:
$('#button-reg').on('click', function () {
if($('.input-name-1').val() != '' && $('.input-name-2').val() != '' && $('.input-phone').val() != '' && $('.input-address').val() != ''){
$.ajax({
url: "mail.php",
type: "POST",
dataType: "JSON",
data: {
'name1' : $('.input-name-1').val(),
'name2' : $('.input-name-2').val(),
'phone' : $('.input-phone').val(),
'address' : $('.input-address').val()
},
cache: false,
success: function (data) {
$('.message').html('Your order was successfully sent');
$('.input-name-1').val('');
$('.input-name-2').val('');
$('.input-phone').val('');
$('.input-code').val('');
$('.input-address').val('');
}
});
}
else{
$('.message').html('Error filling fields');
}
});
PHP:
<?php
$method = $_SERVER['REQUEST_METHOD'];
$name1 = trim($_POST['name1']);
$name2 = trim($_POST['name2']);
$phone = trim($_POST['phone']);
$address = trim($_POST['address']);
if($name1 && $name2 && $phone && $address){
$message = "
First_name: $name1 <br>
Last_name: $name2 <br>
E-mail: $address <br>
Phone: $phone <br>
";
}
function adopt($text) {
return '=?UTF-8?B?'.base64_encode($text).'?=';
}
$headers = "MIME-Version: 1.0" . PHP_EOL .
"Content-Type: text/html; charset=utf-8" . PHP_EOL .
'From: '.adopt($name1).', '.adopt($name2).' <'.$address.'>' . PHP_EOL .
'Reply-To: '.$phone.'' . PHP_EOL;
mail('83971andrey@gmail.com', adopt('Dexe Sar'), $message, $headers );
echo json_encode(array(
'result' => 1
));