0

I want to submit a form with ajax on ajax success that means i don't want to reload the page when the form is submitted on ajax success. I tried

if (response.success == true ) {
$form = $("#formname");
var form = $form.serializeArray();
form.push({
   name: window.csrfTokenName,
   value: window.csrfTokenValue
})

The page didnt reload on this but, the form was also not submitted. I also tried document.form['formname'].submit() but that reloaded the page. Please Help

hashtagerrors
  • 239
  • 4
  • 22
  • You need to write a `AJAX` request with passing `form` data – Rana Ghosh Mar 29 '17 at 08:21
  • You obviously didn't read enought about Ajax. Ajax `success` callback occurs when the resquest has been done. So the "submit" is done and an answer has been received back. You should pass your values to Ajax before the request happens. – Louys Patrice Bessette Mar 29 '17 at 08:22

1 Answers1

1

If you want to submit a form without reloading the page, you have to use Ajax requests. At the same time you have to prevent browser from native form submission. To prevent from submission use something like this

$('yourbuttonselector').click(function(e) {
    e.preventDefault();
});

And for form submission without reloading use Ajax - $.post() for example.

aprok
  • 1,147
  • 11
  • 25