Below is some javascript validation I have on a simple form. The first time I hit submit the validation will work and fire and error messages. If I get an error, enter in new values that are still incorrect and hit submit again the validation does not work anymore and the values are passed to my php form.
Here is the javascript
<script type="text/javascript" language="javascript">
<!--
function validateMyForm ( ) {
var isValid = true;
if ( document.form1.gamedate.value === "Select One" ) {
document.getElementById("errorMessage").innerHTML = ( "Please select a game" );
isValid = false;
} else if ( document.form1.wteam.value === "Select One" ) {
document.getElementById("errorMessage").innerHTML = ( "Please select the winning team" );
isValid = false;
} else if ( document.form1.wscore.value === "Select One" ) {
document.getElementById("errorMessage").innerHTML = ( "Please select the winning score" );
isValid = false;
} else if ( document.form1.lscore.value === "Select One" ) {
document.getElementById("errorMessage").innerHTML = ( "Please select the losing score");
isValid = false;
} else if ( document.form1.wscore.value <= document.form1.lscore.value ){
document.getElementById("errorMessage").innerHTML = ( "Winning score must be larger than the losing score");
isValid = false;
}
return isValid;
}
//-->
</script>
php form with ******* to remove certain data
<form action="*********" method="post" name="form1">
<div class="form-group">
<label>Game Date:</label>
<select name="gamedate">
<option>Select One</option>
<?php
$stmt = $db->prepare("***********)");
$stmt->execute(array(':**********' => $_SESSION['**********']));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="';
echo $row['*********'];
echo '">';
echo $row['*********'];
echo '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label>Winning Team</label>
<select name="wteam">
<option>Select One</option>
<option value="1">Home</option>
<option value="0">Opponent</option>
</select>
</div>
<div class="form-group">
<label>Winning Score</label>
<select name="wscore">
<option>Select One</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
</div>
<div class="form-group">
<label>Losing Score</label>
<select name="lscore">
<option>Select One</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
</div>
<div class="form-group">
<input type="submit" value="Submit" onclick="javascript:return validateMyForm();">
</form>
Any thoughts on what I am doing wrong?
javascript:from theonclick.onclickalready triggers javascript.