The essence of the question in the question itself: it is necessary to add a capture of UTM tags to the feedback form (Ajax) and accordingly make their transfer to the mail. I added, but for some reason they do not come to the post office. Here is my code:

HTML code

<form class="form-1" id="form-1"> <div class="border__frame"> <div class="heading">Отправьте <span>заявку на марс!</span> </div> <div class="description">И наш марсианский менеджер перезвонит вам в ближйшее время.</div> <input type="hidden" name="utm_source" value="<?php echo isset($_GET['utm_source']) ? $_GET['utm_source'] : '' ;?>"> <input type="hidden" name="utm_medium" value="<?php echo isset($_GET['utm_medium']) ? $_GET['utm_medium'] : '' ;?>"> <input type="hidden" name="utm_campaign" value="<?php echo isset($_GET['utm_campaign']) ? $_GET['utm_campaign'] : '' ;?>"> <input type="hidden" name="utm_term" value="<?php echo isset($_GET['utm_term']) ? $_GET['utm_term'] : '' ;?>"> <input type="hidden" name="utm_content" value="<?php echo isset($_GET['utm_content']) ? $_GET['utm_content'] : '' ;?>"> <input type="hidden" name="formid" value="с первого экрана Pop-up"> <input type="text" name="name" placeholder="Ваше имя" required> <input class="index__phone" name="code" type="tel" maxlength="3" placeholder="123" required> <input class="number__phone" name="tel" type="tel" placeholder="325-54-94" required> <button class="btn-submit-mars" type="submit">Отправить заявку на марс</button> </div> </form> 

PHP handler

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

js

 $("#form-1").validate({ rules: { name: { required: true, minlength: 2 }, code: { required: true, digits: true, minlength: 3, maxlength: 3 }, tel: { required: true, minlength: 7, maxlength: 9 } }, messages: { name: { required: "Неверно заполнено поле :(", minlength: "Минимальное кол-во символов 2" }, code: { required: "Введите код", digits: "Обязательно цифры", minlength: "3 символа" }, tel: { required: "Введите номер телефона :(", 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'); }, 5000); }); } return false; }); 

In principle, nothing seems to be complicated, but it does not work. A letter comes to the form: and wherever it is. Source: .... Keyword: .... the form itself is working and tested more than once. Tell or show how to correctly set these tags in the form, to be honest, google rummaged, used examples - but there’s no use. Thank.

  • On the Internet, I stumbled upon the information that the request was basically created correctly, but the tags on the site themselves are set up, respectively, no capture takes place, and as a result, empty fields. - sagan
  • Hello! You were able to send UTM tags, in the form of feedback, I have the same question? UTM tags are not transmitted. - ssn

1 answer 1

In my opinion, there were several errors in this code. In the example below, they should be corrected:

 $("#form-1").submit(function(e) { e.preventDefault(); // prevent doubly sending var form = $(this); if (form.valid()) { $.post({ "mail.php", form.serializeArray().reduce(function(o, a) { o[a.name] = a.value; return o; }, {}), // post an object instead of string function(data) { // response from server is here form.submit(function(e) { e.preventDefault(); // no further submits allowed }); $(":submit", form).prop("disabled", true); $(".success").addClass("visible"); setTimeout(function() { form.trigger("reset").hide(); $(".success").removeClass("visible"); }, 1000); $('.mfp-bg.mfp-ready').css({ 'display': 'none' }); $('#popUpMessage').removeClass('hiddenDiv'); setTimeout(function() { $('#popUpMessage').addClass('hiddenDiv'); }, 5000); } }); } }); 
  • Thanks, but these errors, as you say, do not affect the resolution of my question at all, I said at first: the form is completely working, I just added to it the capture of utm-tags that are no longer displayed in the email. - sagan
  • @sagan, and if the form is not sent via Ajax, then what? - DiGiTAL
  • I do not understand what the question is? what does not mean via ajax? I did it just through ajax, and the customer was in favor of who would change along the way? - sagan 2:41 pm
  • Does the form work, if you directly submit the form, and not ajax? have the necessary data in the mail? - DiGiTAL
  • Another way to debug is to look at the output of print_r($_POST) is everything sent there exactly? - DiGiTAL