There is a landing page on which there are 5 feedback forms, they are almost the same, but the customer asks to do so that the information would come to the mail from which particular form the order came, how to implement it?

Sample code for one of the forms:

<form class="form-1" id="form-1"> <div class="border__frame"> <div class="heading">Отправьте <span>заявку на марс!</span> </div> <div class="description">И наш марсианский менеджер перезвонит вам в ближйшее время.</div> <input type="text" name="name" maxlength="30" placeholder="Ваше имя" required> <input class="index__phone" name="code" type="tel" maxlength="3" placeholder="123" required> <input class="number__phone" name="tel" type="tel" maxlength="10" placeholder="325-54-94" required> <button class="btn-submit-mars" type="submit">Отправить заявку на марс</button> </div> </form> 

  $("#form-1").validate({ rules: { name: { required: true, minlength: 2 }, code: { required: true, digits: true, minlength: 3, maxlength: 3 }, tel: { required: true, digits: true, minlength: 7 } }, messages: { name: { required: "Неверно заполнено поле :(", minlength: "Минимальное кол-во символов 2" }, code: { required: "Введите код", digits: "Обязательно цифры", minlength: "3 символа" }, tel: { required: "Введите номер телефона :(", digits: "Обязательно цифры", minlength: "Минимальное кол-во символов 7" } } }); $("#form-1").submit(function() { if ($("#form-1").valid()) { var th = $(this); $.ajax({ type: "POST", url: "mail.php", data: th.serialize() }).done(function() { $(".success").addClass("visible"); setTimeout(function() { // Done Functions th.trigger("reset"); $(".success").removeClass("visible"); }, 1000); $('#form-1')[0].reset( setTimeout(function() {}, 1000) ); $("#form-1").hide(); $('.mfp-bg.mfp-ready').css({ 'display': 'none' }); $('#popUpMessage').removeClass('hiddenDiv'); setTimeout(function() { $('#popUpMessage').addClass('hiddenDiv'); }, 4000); }); } return false; }); 

and the mail.php handler itself

 <?php $recepient = "xxxxxxxxxxxxx@gmail.com"; $sitename = "Марсоход"; $name = trim($_POST["name"]); $code = trim($_POST["code"]); $tel = trim($_POST["tel"]); $message = trim($_POST["message"]); $message = "Имя: $name \nТелефон: $code $tel \nВопрос: $message"; $pagetitle = "Новое сообщение с сайта \"$sitename\""; mail($recepient, $pagetitle, $message, "Content-type: text/plain; charset=\"utf-8\"\n From: $recepient"); 

I am interested in: what is the need for a separate handler for each form, if not then how does it fit into one, and what would these headers-tags about the pages display there? Help advice. Thank.

    1 answer 1

    There are many options, for example

    1. add to the form a hidden field with the name of the form
    2. Add to js when sending to data either the current url or again the name of the form (I like this option more)

    UPD

      var th = $(this).serialize(); th.push({name: 'form_name', value: 'name'}); th.push({name: 'form_url', value: window.location.pathname}); $.ajax({ type: "POST", url: "mail.php", data: th }).done(function() { 

    Taken from here https://stackoverflow.com/questions/6627936/jquery-post-with-serialize-and-extra-data

    • Can you give more details about the second version? not very strong in js, and it's hard to understand how to implement it right away - sagan
    • Now letters are not sent - sagan