简体   繁体   中英

PHP onSubmit, JS function will not execute

I am still new to PHP and JS, but from what I have been reading on, when a form has an onSubmit element, then the form requires a return true from the function in order to execute the submission. I cannot get this to work however.

Can someone help me figure out what my problem is? I also added a random alert() at the beginning of the Javascript function and that does not go off either.

Thank you!

<HTML>
<HEAD>


<script type="text/javascript">

function Validate(){
    alert("validating..");
    var min=document.forms["myForm"]["minWeight"].value;
    var max=document.forms["myForm"]["maxWeight"].value;

if (min < max)
  {
      return true;
  }
  else 
  {
  alert("Minimum weight " + min + " is not lower than " + max " Maximum weight");
      return false;
  }
}
</script>

</HEAD>
<BODY>

<form name="myForm" action="page2.php" method="post"  onSubmit="return Validate()">
    <p><label>Enter minimum weight: <input type="number" name="minWeight" value="0" min="0" required /></label></p>
    <p><label>Enter maximum weight: <input type="number" name="maxWeight" value="999" min="0" max="999" required /></label></p>
    <p>Select gender:
        <label><input type="radio" name="genderType" value="male" required /> Male</label>
        <label><input type="radio" name="genderType" value="female" /> Female</label>
    </p>
    <p><input type="submit" value="Submit" /></p>
</form>

</BODY>
</HTML>

You have a typo in your javascript:

alert("Minimum weight " + min + " is not lower than " + max " Maximum weight");
                                                          ^^^

Change it to:

alert("Minimum weight " + min + " is not lower than " + max + " Maximum weight");

and it should work, see the example on jsbin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM