Hello. The task is as follows.
There is a page with a certain number of pop-ups (with the same class) containing different forms.
Inside each form there is an empty
<div id="result"></div> which will receive and display the server response after submitting the form. The forms themselves are essentially identical, only different fields.
How to make a selection correctly, so that when submitting, the answer is displayed in #result, which is in the form that the user sent, and not in the first page that was found?
submission script:
$('.formAjax').submit(function(event) { /* отключение стандартной отправки формы */ event.preventDefault(); /* собираем данные с элементов страницы: */ var $form = $( this ), h1_form = $('h1').text(), h2_form = $('h2').text(), link = $form.find( 'input[name="url"]' ).val(), ajax = $form.find( 'input[name="ajax"]' ).val(), name = $form.find( 'input[name="name"]' ).val(), phone = $form.find( 'input[name="phone"]' ).val(), diapazon = $form.find( 'select[name="diapazon"]>option' ).val(), url = $form.attr( 'action' ); /* отправляем данные методом POST */ var posting = $.post( url, { h1: h1_form, h2: h2_form, url: link, ajax: ajax, name: name, phone: phone, diapazon: diapazon } ); /*после отправки формы*/ posting.done(function( data ) { /*выводим результат запроса*/ // alert( data ); // alert('Вы записались на прием, мы скоро свяжемся с вами'); // $('form').html(result); // $( "#result" ).empty().append( data ); $( "#result" ).text(data); console.log ('ok'); /*очищаем поля формы*/ $(':input','form') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .removeAttr('selected'); }); }); form example
<form action="/algo/addappointment.php" class="formAjax"> <input type="text" placeholder="Ваше имя*" name="name" required class="form__input form__input_popup"/> <input type="text" placeholder="Телефон*" name="phone" required class="form__input form__input_popup"/> <input type="hidden" name="url" required value="<?=$APPLICATION->GetCurDir()?>"/> <input type="hidden" name="h1" required id="h1_form" value=""/> <input type="hidden" name="ajax" required value="y"/> <select name="diapazon" class="form__input form__input_popup"> <option value="9-12">С 9 до 12</option> <option value="12-15">С 12 до 15</option> <option value="15-18">С 15 до 18</option> <option value="18=21">С 18 до 21</option> </select> <input type="submit" value="Записаться" class="button button_big"/> <div id="result"></div> </form>