There are several forms on the land. There was a need to add the ability to attach a file to one of them.

Now without a file, everything works like this.

Sample form:

<form action="" method="post"> <input type="hidden" class="type" value="zamer" name="type"> <input class="name" name="name" type="text" placeholder="Ваше имя"> <input class="tel" name="tel" type="text" minlength="13"> <input type="file" name="file" id="file"> <button>ОСТАВИТЬ ЗАЯВКУ</button> </form> 

Part of the script:

 $('form').submit(function(e){ e.preventDefault(); var form_data = { 'name':$(".name", $(this)).val(), 'tel':$(".tel", $(this)).val(), 'square':$("#square", $(this)).val(), 'type':$(".type", $(this)).val(), }; $.ajax({ type: "POST", url: "mail.php", data: form_data, success: function(){ $('.modal').modal('hide'); setTimeout(function() { $('.success').fadeToggle(); }, 1000); setTimeout(function() { $('.success').fadeToggle(); }, 2500); }, error: function() { alert("Произошла какая то ошибка!"); } }); }); 

And, actually, mail.php

 if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST['name'])) {$name = $_POST['name'];} if (isset($_POST['tel'])) {$phone = $_POST['tel'];} if (isset($_POST['square'])) {$square = $_POST['square'];} $to = "mail@mail.ru";/ $subject = "Сообщение с лендинга site.ru"; $subject = "=?utf-8?B?".base64_encode($subject)."?="; $message = ' <h3>Сообщение с лендинга site.ru</h3> <p>Площадь кухни:<b> '.$square.'</b></p> <p>Имя отправителя:<b> '.$name.'</b></p> <p>Телефон: <b>'.$phone.'</b></p>'; $headers .= 'from: mail2@mail.by' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; if(mail($to, $subject, $message, $headers)){ exit("Спасибо за заказ"); } } 

What and where to write, so that the file came in the mail? I myself am not a programmer and did everything at random.

Huge request not to give links. Please write in detail, as far as possible, for a "full kettle!"

1 answer 1

I am writing in detail:

 var form_data = new FormData(this); 

instead

 var form_data = { 'name':$(".name", $(this)).val(), 'tel':$(".tel", $(this)).val(), 'square':$("#square", $(this)).val(), 'type':$(".type", $(this)).val(), }; 

Php

 $_FILES['file']['tmp_name'] 
  • And where, before or instead of what to insert it? And this is in the script. And what to add and mail.php? - Concept
  • Mail stopped coming in general - Concept