php - How to stop form from submitting using JqueryAjax - Stack Overflow

admin2025-04-19  0

This is the code I have. I want to prevent the form from submitting if the message i receive from the php side is 0 (an error occured). If the message received is 1 I want to submit the form.

   $(document).ready(function(){
    $('#form').submit(function(e){
        register();


    });

});


    function register(){

        $.ajax({
            type:"POST",
            url:"submit.php",
            data: $('#form').serialize(),
            dataType: "json",
            success: function(msg){

                if(parseInt(msg)==1){
                    window.location=msg;
                }
                else if(parseInt(msg)==0){  
                    $('#error_out').html(msg);
                }


            }

        });
    }

This is the code I have. I want to prevent the form from submitting if the message i receive from the php side is 0 (an error occured). If the message received is 1 I want to submit the form.

   $(document).ready(function(){
    $('#form').submit(function(e){
        register();


    });

});


    function register(){

        $.ajax({
            type:"POST",
            url:"submit.php",
            data: $('#form').serialize(),
            dataType: "json",
            success: function(msg){

                if(parseInt(msg)==1){
                    window.location=msg;
                }
                else if(parseInt(msg)==0){  
                    $('#error_out').html(msg);
                }


            }

        });
    }
Share asked Jul 11, 2011 at 18:05 user461316user461316 9133 gold badges12 silver badges31 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

after register(); include return false;

 $('#form').submit(function(e){
           e.preventDefault();

           //do whatever you want

});

As I understand ,Try use this:

if(parseInt(msg)==0){ $('#submit').attr('disabled','true'); }

after register(); include return false; if it doesn't work then rename register(); to register(e); and then include e.preventDefault(); e.stopPropagation(); hope it works.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745054177a282297.html

最新回复(0)