It is necessary to send data from the form to the mail. How to do it right?
The form:
<form class="ajax_form" action="ajax_mail.php" method="get"> <h2>Связаться с нами</h2> <label> <span>Ваше имя:</span> <input type="text" name="uname" required/> </label> <label> <span>Ваш телефон:</span> <input type="text" name="phone" required/> <input type="text" name="email" placeholder="mail" required/> </label> <label> <span>Ваш город:</span> <select name="city"> <option>Москва</option> <option>С-Петербург</option> </select> </label> <div class="button_wrap"> <button class="button btn_green" type="submit">Отправить заявку</button> </div> </form>
Js:
$(".ajax_form").on('submit', function(e) { var formData = $(".ajax_form").serialize(); e.preventDefault(); $.ajax({ type: "GET", url: "../mail_ajax.php", dataType: 'json', data: formData }).done(function() { alert("Спасибо за заявку!"); }); });
PHP:
<?php header("Content-Type: application/json", true); error_reporting(-1); ini_set('display_errors', 'On'); set_error_handler("var_dump"); json_encode($_GET['uname']); $to = "user@gmail.com"; $name = $_GET['uname']; $phone = $_GET['phone']; $city = $_GET['city']; $email = $_GET['email']; $subject = "Name:" . $name . " , City:" . $city . " , Phone:" . $phone; $message = "message"; $headers = "From:" . $email; mail($to, $subject, $message, $headers); ?>