3

I have the Code like this which checks code availability if code exists the form does not submit

i have the code which is not working

<script type="text/javascript">
$(document).ready(function() {
    $('#Loading').hide();
            var min_chars = 3;  
            //result texts  
            var characters_error = 'Minimum amount of chars is 3';  
            var checking_html = 'Checking...';      


        $('#reality_form').submit(function(){

            if($('#realitycode').val().length < min_chars){
                $('#Info').html(characters_error);  
            }else{  
                //else show the cheking_text and run the function to check  
                    $('#Info').html(checking_html);  
                        var reltcode = $('#realitycode').val();  

                //use ajax to run the check  
                $.post("http://localhost/Testtt/wp-content/plugins/Realestate Redirection/check_realitycode_availablity.php", { realitycode: reltcode },

                 function(result){  
                    //if the result is 1  
                    if(result == 1){  
                        //show that the username is available
                        alert('hi');  
                        $('#Info').html(reltcode + ' is Available'); 

                    }else{  
                        alert('hello'); 
                        $('#Info').html(reltcode + ' is not Available');

                    } 

                }); 
           return false;    
            }

    });  

 }); 

</script>
<div class="wrap"> 
            <?php    echo "<h2>" . __( 'RealEstate Listing Options', 'oscimp_trdom' ) . "</h2>"; ?>
            <form action="" name="reality" method="post" id="reality_form">
            <input type="hidden" name="reality_hidden" value="Y">  
            Website Url:<input type="text" name="website_url" value="" />
            Listing Code: <input type="text" name="rlt_code" id="realitycode" />
            <input type="submit" name="submit" value="submit" />
            <div id="Info"></div>
            <span id="Loading"><img src="http://localhost/Testtt/wp-content/plugins/Realestate Redirection/loader.gif" alt="" /></span>

            </form>
</div>

As i look on console the Ajax url becomes error but when i use for realitycode keyup() function then Ajax works properly. i want form should not get submit if any error. but form is submitting in every case Please help

11
  • your return false/true is inside inner function.so it can not return false for submit function for preventing it from submmiting. Commented Jun 9, 2012 at 9:44
  • i have posted the answer.try and check if that help? Commented Jun 9, 2012 at 9:55
  • when i put it as said after Ajax then it stopped in every case code $.post("localhost/Testtt/wp-content/plugins/Realestate Redirection/check_realitycode_availablity.php", { realitycode: reltcode }, function(result){ //if the result is 1 if(result == 1){ //show that the username is available alert('hi'); $('#Info').html(reltcode + ' is Available'); }else{ alert('hello'); $('#Info').html(reltcode + ' is not Available'); } }); return false; code Commented Jun 9, 2012 at 10:00
  • can you post the edited code in your question? Commented Jun 9, 2012 at 10:07
  • i just edited the code & pasted Commented Jun 9, 2012 at 10:10

5 Answers 5

2

Use the .click event instead of submit. Then if the form is valid, call submit yourself in code.

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

Comments

1

i want form should not get submit if any error. but form is submitting in every case Please help

Put return false at the end of your $('#reality_form').submit() function:

$('#reality_form').submit(function(){
   // your other code
   return false;
});

1 Comment

it gets stop now in everycase when i put code return false; code in end of function as said can you show through code where to put
0

The other way to prevent from submitting is using the following code:

$('#reality_form').submit(function(e){

    e.preventDefault();

In jQuery each handler receives event object as the first parameter and this object has method prevetDefault() which suppresses the default behaviour. It could be used with any kind of event, such as click on <a> element.

Comments

0

make following change to your code:

     <script type="text/javascript">
   var validated = false;
    $(document).ready(function() {

        $('#Loading').hide();
                var min_chars = 3;  
                //result texts  
                var characters_error = 'Minimum amount of chars is 3';  
                var checking_html = 'Checking...';      


            $('#reality_form').submit(function(){
                if(validated)
                    return true;
                if($('#realitycode').val().length < min_chars){
                    $('#Info').html(characters_error);  
                }else{  
                    //else show the cheking_text and run the function to check  
                        $('#Info').html(checking_html);  
                            var reltcode = $('#realitycode').val();                   

                    //use ajax to run the check  
                    $.post("http://localhost/Testtt/wp-content/plugins/Realestate Redirection/check_realitycode_availablity.php", { realitycode: reltcode },

                     function(result){  
                        //if the result is 1  
                        if(result == 1){  
                            //show that the username is available
                            alert('hi');  
                            $('#Info').html(reltcode + ' is Available'); 
                            //do nothing

                        }else{  
                            alert('hello'); 
                            $('#Info').html(reltcode + ' is not Available');
                            validated = true;
                            $('#reality_form').submit();
                        }   
                    }); 
                  return false;    
                }

        });      

     }); 

    </script>

1 Comment

solution inferred from this link : stackoverflow.com/questions/2637985/…
0

I came up with the same problem. The solution is...

Dont use name="submit"
<input type="submit" name="submit" value="submit" />

Instead
<input type="submit" name="anyOtherName" value="submit" />

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.