user5723483
user5723483

Reputation:

Unknown error using AJAX to stay in the same page

Following this previous link, I tried a new AJAX code, but still getting the same result (Data is inserted correctly, but can't stay in the same page).

$(function(){
    $("#form1").on("submit", function(e){
        $.ajax({
          url: 'insert.php',
          type: 'post',
          data: $('#form1').serialize(),
          dataType: 'json',
          success: function(data) {
              alert($('#form1').serialize());
         });
         e.preventDefault();
    });
});

enter image description here

The form has an id="form1" and submit button inside this form is id="insert".

Any help is appreciated, need to fix this today.

Upvotes: 2

Views: 151

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

The success function should be closed before closing AJAX function. You forgot to add a closing }:

$(function(){
  $("#form1").on("submit", function(e){
    $.ajax({
      url: 'insert.php',
      type: 'post',
      data: $('#form1').serialize(),
      dataType: 'json',
      success: function(data) {
        alert($('#form1').serialize() + "&insert=yes"); // add this
      } //<--------------------------------------- You missed this
    });
    e.preventDefault();
  });
});

You are sending the insert key as a parameter to post, but it doesn't work because you are not adding it while sending to AJAX. So add that and it works.

Upvotes: 2

Related Questions