How to transfer data using a post without a form, at the same time so that the user is transferred to the page on which we are transmitting data?

Steps: 1. The client fills out the form 2. The entered data is processed 3. Depending on the entered data, the url is selected to which the user will be redirected and the data sent in the form of post.

    3 answers 3

    If I understand you correctly, then somehow

    $('#f-form__callback').on('submit', function(e){ var fdata = $(this).serialize(); var ajaxUrl = ''; e.preventDefault(); // блокируем стандартную отправку формы // ваша обработка данных в которых вы определяете куда отправить данные // в зависимости от ваших обработок устанавливаете ajaxUrl if (someVar = 'someValue') ajaxUrl = '/some/url'; if (ajaxUrl !== '') { // если вы поняли куда отправлять данные, то делаем это через ajax $.ajax({ url: ajaxUrl, type: 'post', dataType: 'text', data: fdata, success: function (data) { // в data содержится адрес, куда вы будете перенаправлены, если у вас есть эта обработка на сервере if (data) { window.location.href = data; } else { window.location.href = ajaxUrl; } } }); } }); 

      Change the action the parent form depending on the data entered, and send it as standard by the post method. I highly recommend that I do not throw the Ajax into the submit event, and then redirect the user using location.href , as advised by the comrades below.

      • Share, please, why "extremely not recommend"? - Peresada
      • Because you are putting asynchronous code in synchronous and it is a bad practice. Sometimes they do this when it’s really justified; here, a friend needs to simply replace the action . - ferrari
      • You have not explained why this is a bad practice. In terms of user loyalty, this is absolutely not true - Peresada
       $('html').html('<form action="/2.php" name="vote" method="post" style="display:none;"><input type="text" name="name" value="яндекс" /></form>'); document.forms['vote'].submit(); 

      Did so.