views:

39

answers:

2

Hello All,

I am using the jquery to validate each field in my form and also using the jquery to show a "loading" message after submitting the form. Now, in my case I got confused when I clicked on the submit button the validation messages will be shown and also the loading message. I need only the loading message to be shown once everything is correct.

<html lang="en"> 
    <head> 
        <title>SO question 3377468</title> 
        <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt; 
        <script> 
            $(document).ready(function() { 
                $('#form').submit(function() { 
                    $('#progress').show(); 
                }); 
            }); 
        </script> 
        <style> 
            #progress {  
                display: none; 
                color: green;  
            } 
        </style>             
    </head> 
    <body> 
        <form id="form" action="servlet-url" method="post"> 
            ... 
            <input type="submit"> 
        </form> 
        <div id="progress">Please wait...</div> 
    </body> 
</html> 

http://stackoverflow.com/questions/3377468/how-to-show-jquery-message-after-clicking-on-submit-button

I took this code when one of the colleagues had helped and now I want to complete this.

A: 

Possibly something like:

<input type="button" value="SUBMIT" onclick="validateFunction()">

Then in your validateFunction, at the end if it is all good to go then call document.form.submit() which will process your loading dialog and send the form.

Also, as a side note, since you stated you are using jquery for validation, with your current setup a person with javascript disabled could still submit invalid data (causing all sorts of drop tables and whatnot). With this method (the submit being called within the script), they won't be able to send AT ALL.

Another route is:

<input type="submit" onclick="validateFunction();return false;">

And at the end of your validate function set it to return(true/false) if it is okay to send or not. THEN re-validate on the server side, using PHP or whatever and return an error if any problems. This allows users without javascript to still attempt to send but not have them destroy your site.

WSkid
I would suggest putting a .click on that submit button opposed to putting the onclick function call in the input attribute itself. Makes for easier code debugging.
Chris
Still I got both messages
maas
+1  A: 

If you're using the validation plugin (I'm unclear on this from the question, but it's tagged that way) just move your code around a bit. Instead of this:

$(document).ready(function() { 
    $('#form').submit(function() { 
        $('#progress').show(); 
    }); 
});

You'll want to use the submitHandler option of the plugin, this function only runs after a form has successfully passed validation and is being submitted, like this:

$("#form").validate({
  //other options...
  submitHandler: function() {
    $('#progress').show(); 
  }
});

This means just remove that first block of code, the submitHandler code is all you should have left for the #progress stuff.

Nick Craver