<!-- Код для проброса значений из формы в виде ссылки на страницу успеха -->
<!-- https://roman-kosov.ru/form-values-by-link -->
<script>
function updateSuccessUrlWithParams(form) {
var fields = '?';
var id = form.getAttribute('id');
var formPrevCallback = form.getAttribute("data-success-callback");
var formSuccessCallback = formPrevCallback + '_' + id;
form.setAttribute("data-success-callback", formSuccessCallback);
window[formSuccessCallback] = function() {
var serializeArray = new FormData(form);
for (var [key, value] of serializeArray) {
if (key.indexOf("form-spec-") === -1 && (key.indexOf("tildaspec-") === -1 || key === "tildaspec-phone-part[]") && key.indexOf("formservices") === -1) {
if (value !== "") {
if (key === 'phone') {
fields += key + '=' + value.replace(/[\s+\-\(\)]/g, '').trim() + '&';
} else {
fields += key + '=' + value + '&';
}
}
}
}
var length = fields.length;
var utms = window.location.search.split("&").filter(val => val.indexOf('s_') !== 0 && val.indexOf('utm_') !== -1).join("&").replace('?', '');
var resultCallbackURL = form.getAttribute("data-success-url");
if (fields !== '?') {
resultCallbackURL += fields.substring(0, length-1);
}
if (utms) {
resultCallbackURL += '&' + utms;
}
form.setAttribute("data-success-url", resultCallbackURL);
if (typeof window[formPrevCallback] === 'function') {
window[formPrevCallback](form);
}
};
};
t_onReady(function() {
/* Находим формы из обычных блоков */
var allSimpleForms = document.querySelectorAll(".js-form-proccess[data-success-url]:not([data-success-callback='t396_onSuccess']");
Array.prototype.forEach.call(allSimpleForms, function (form) {
updateSuccessUrlWithParams(form);
});
/* Находим формы в Zero block'ах */
var allZeroForms = document.querySelectorAll('.tn-elem[data-elem-type="form"]');
Array.prototype.forEach.call(allZeroForms, function(form) {
form.addEventListener('render', function(event) {
if (event.target) {
var form = event.target.querySelector('form');
if (form) {
updateSuccessUrlWithParams(form);
}
}
});
});
});
/* Эта функция отвечает за переадресацию из форм в Zero block'ах, её наличие обязательно для переадресации */
t396_onSuccess = function (form) {
if (form instanceof jQuery) {
form = form.get(0);
}
var successUrl = form.getAttribute('data-success-url');
if (!successUrl) {
successUrl = $(form).data('success-url')
}
if (successUrl) {
setTimeout(function () {
window.location.href = successUrl;
}, 500)
}
};
</script>