Good day. Faced a problem. On the page there is a form:

<form id="recoverform" class="loginform" action=""> <input type="email" name="email" placeholder="Email"> <button class="btn" onclick="sandEmail();" name="sandBtn">Отправить</button> <p>Сообщение с данными для входа будет отправлено на ваш Email</p> </form> 

It is sent via ajax request:

 function sandEmail(){ $.ajax({ url:'sendMessage.php', type:'post', data: $('#recoverform').serialize(), success: function(result){ $(sendForm).next().html(result); } }); 

} Pressing the button will do the following. A page opens with the same url: http://hostname.ru/login?email=&sandBtn=

What's wrong? Why is the data transmitted not by the POST method, but by GET and why on the current page, and not on the php file without updating the current page?

  • And the values ​​are entered? sandBtn goes without a value field, entering a mail box in question ... If you just press a button, then the behavior is quite correct ... - DNS
  • @DNS No, the data is not sent to the file handler, they are passed to the current page by the gett. - Alexey Vladimirovich
  • Yes, I hurried, I didn’t look at everything - you suspend the handler not on the form, but on the button. The POST method concerns the form itself ... - DNS

1 answer 1

I have corrected your initial code, which you very quickly changed. All you need here is a form submission handler that is canceled using event.preventDefault(); , with ajax-a package.

 <form id="recoverform" class="loginform" action=""> <input type="email" name="email" placeholder="Email"> <button class="btn" name="sendBtn">Отправить</button> <p>Сообщение с данными для входа будет отправлено на ваш Email</p> </form> $(document).ready(function() { $('#recoverform').submit(function(event){ event.preventDefault(); var sendForm = this; $.ajax({ url:'sendMessage.php', type:'post', data: $(sendForm).serialize(), success: function(result){ $(sendForm).next().html(result); } }); }); }); 
  • I apologize, but what have you changed?) - Alexey Vladimirovich
  • event.preventDefault(); - vp_arth
  • Hah, guys, I wildly apologize, in short I did not connect ajax to the page) It's time to drink coffee) I will mark the answer as correct, thank you - Alexey Vladimirovich