Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: ajax POST inside a form - prevent form submission on ajax call

Tags:

jquery

I am trying to perform an ajax call inside a form (a Drupal node edit form) , but it seems when performing the call, it submits the form for some reason. Here is a sample code:

jQuery.ajax({
     type: "POST",
     url: "my_custom/url",
     dataType: "html",
     data: {"text": jQuery("#edit-body").html()
      },
     success: function(result){
        console.log(result);
     }
    });  

I can replicate this just by executing it in the console, but I attach this to a button click function inside the form. Any tips on preventing the form from submitting, on a POST ajax call?

Here is the full code as requested

        jQuery("#edit-body").before('<div id="proofread_bot-button-holder"><button type="button"  id="proofread_bot-submit" onclick="return false;">Check with Proofread Bot</button></div>'); 
    jQuery("#proofread_bot-submit").click(function(event){
      event.preventDefault();
      jQuery("#proofread_bot-button-holder").append("<img id=\"proofread_bot_throbber\" src=\"sites/all/modules/proofread_bot/images/throbber.gif\" />");

      jQuery.ajax({
         type: "POST",
         url: "proofread_bot/check",
         dataType: "html",
         data: {"text": jQuery("#edit-' . variable_get('proofread_bot_field') . '").html()
          },
         success: function(proofread_result){
            jQuery("#proofread_bot-submit").after(proofread_result);
            jQuery("#proofread_bot_throbber").remove(); 
         }
        });    
      });
like image 820
giorgio79 Avatar asked Nov 14 '11 12:11

giorgio79


People also ask

How do I stop a form from submitting ajax?

To submit a form without Ajax, you can either disable Ajax form handling globally, or per form via the data-ajax="false" attribute. The target attribute (as in target="_blank" ) is respected on forms as well, and will default to the browser's handling of that target when the form submits.

How do I stop a form from submitting in jQuery?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting.

How do you stop a form from getting submitted?

The simplest solution to prevent the form submission is to return false on submit event handler defined using the onsubmit property in the HTML <form> element.

How Prevent form submit on Enter key press jQuery?

addEventListener("submit",function(e){e. preventDefault(); return false;}); This solution will now prevent the user from submit using the enter Key and will not reload the page, or take you to the top of the page, if your form is somewhere below.


2 Answers

You need to override form's onsubmit event to prevent submitting:

$("formSelector").bind('submit', function (e) {
    var isValid = someYourFunctionToCheckIfFormIsValid();
    if (isValid) {
        jQuery.ajax({
            type: "POST",
            url: "my_custom/url",
            dataType: "html",
            data: { "text": jQuery("#edit-body").html()
            },
            success: function (result) {
                console.log(result);
            }
        });
    }
    e.preventDefault();
    return false;
});

By calling

e.preventDefault();
return false;

You prevent synchronous postback from occurring.

UPDATE: If you don't want to override form submit, maybe you could place your button outside of form tag (you can adjust position with css if necessary)?

like image 127
Goran Obradovic Avatar answered Oct 23 '22 11:10

Goran Obradovic


If you are using a input type="submit" button, then you need to do a return false; at the end of the function to prevent it from submitting.

Another solution is to e.preventDefault() on the button click

$(".button").click(function(e){
    e.preventDefault();
    return false;
});
like image 3
Niels Avatar answered Oct 23 '22 10:10

Niels