www.webdeveloper.com
Results 1 to 9 of 9

Thread: onsubmit problem

  1. #1
    Join Date
    Dec 2011
    Posts
    79

    onsubmit problem

    hi, am rather confused as to why my on submit is not firing the function i want it to. anyone know why ?


    Code:
    <form name="mail" method="post" action="send_form_email.php" >
    html
    code
    goes
    here
    
    <input type="submit" value="Submit" onsubmit="Checks()">
    
    
    </center>
    
    </form>
    
     function Checks() {
     
     if ( age < 24 && age > 20)  {
      alert("We may be able to insure you, please call us.")};
      return false;
      if ( age < 21 ) {
      alert("You must be over 24 to hire")};
      return false;
     
     }

  2. #2
    Join Date
    Jul 2011
    Posts
    127
    You should write onsubmit event in form tag or onclick event in your input.
    So there should be:
    Code:
    <input type="submit" value="Submit" onclick="Checks()">
    or
    Code:
    <form name="mail" method="post" action="send_form_email.php" onsubmit="Checks()" >

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    Quote Originally Posted by kromol View Post
    Code:
    <form name="mail" method="post" action="send_form_email.php" onsubmit="return Checks()" >
    Don't forget to return the response of your function to cancel/process the submission.

    Also, I will just mention that the Javascript code should be wrapped in <script> tags if it is not already.
    JavaScript: Learn | Validate | Compact

  4. #4
    Join Date
    Feb 2012
    Posts
    218
    Here is a working code
    HTML Code:
    <form name="mail" method="post" action="send_form_email.php" onsubmit="return Checks()">
    html
    code
    goes
    here
    
    <input type="submit" value="Submit">
    
    
    </center>
    
    </form>
    
    <script type="text/javascript">
    	function Checks() {
    		if ( age < 24 && age > 20) {
    			alert("We may be able to insure you, please call us.");
    			return false;
    		}
    		else if ( age < 21 ) {
    			alert("You must be over 20 to hire");
    			return false;
    		}
    	}
    </script>
    I also noticed you made some syntax errors in your javascript. Pay more attention to brackets.

  5. #5
    Join Date
    Dec 2011
    Posts
    79
    Thank you for all the replys, i will test the code later, also the script is not wrapped in the scripttype tags as it is in a seprate file, i just put it there so you could see the function too. thanks you very much

  6. #6
    Join Date
    Dec 2011
    Posts
    79
    Quote Originally Posted by hyperionXS View Post
    Here is a working code
    HTML Code:
    <form name="mail" method="post" action="send_form_email.php" onsubmit="return Checks()">
    html
    code
    goes
    here
    
    <input type="submit" value="Submit">
    
    
    </center>
    
    </form>
    
    <script type="text/javascript">
    	function Checks() {
    		if ( age < 24 && age > 20) {
    			alert("We may be able to insure you, please call us.");
    			return false;
    		}
    		else if ( age < 21 ) {
    			alert("You must be over 20 to hire");
    			return false;
    		}
    	}
    </script>
    I also noticed you made some syntax errors in your javascript. Pay more attention to brackets.

    just tested this and it still wont work, It dosent even fire the function, using google chromes develop tools it just skips over it

  7. #7
    Join Date
    Feb 2012
    Posts
    218
    Of course it does. Where do you set age variable?

  8. #8
    Join Date
    Dec 2011
    Posts
    79
    Got it to work, missed a bracket , thanks for all your help
    Last edited by chrisboots; 02-24-2012 at 08:51 AM.

  9. #9
    Join Date
    Feb 2012
    Posts
    218
    First of all, alert(age) to se if you get the correct age.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles