2

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);
                }


            }

        });
    }

4 Answers 4

5

after register(); include return false;

Sign up to request clarification or add additional context in comments.

10 Comments

I only want to prevent submission when i receive a msg = '1'
Then you would need to make the ajax synchronous. add async: false to the ajax parameters and move the return false; to the success function (under window.location=msg).
+1. fastest answer I have ever seen.. just 38 seconds for answering this question.
It still does not not prevent me using no async and adding the return false;
ah sorry. Change register(); to return register();
|
1
 $('#form').submit(function(e){
           e.preventDefault();

           //do whatever you want

});

Comments

0

As I understand ,Try use this:

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.