javascriptphphtmlonsubmit

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.

Where is my problem? I also added a random alert() at the beginning of the JavaScript function and that does not go off either.

<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>

Solution

  • 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.