Reputation: 13
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?
Upvotes: 0
Views: 183
Reputation: 3005
Javascript validation is great however you should also be doing the same validation steps in your PHP form handler. Then if it does not pass validation from the PHP form handler, you redirect back to the page with your form again. Preferrably pre-filling the valid fields and noting which fields need to be corrected.
Javascript validation should be considered an ADD-ON to server side validation.
@danl Here is some basic PHP validation. I'm not certain what format your gamedate variable should be to check it. The others are very basic.
$haserror=false;
$gamedate=$_POST['gamedate'];
//not sure what is valid for gamedate. But you should check it against something
if(strlen($gamedate)<1){
$haserror=true;
$errordetail.=' Game Date must be selected';
}
$wteam=$_POST['wteam'];
if(!(($wteam==1) || ($wteam==0))){
$haserror=true;
$errordetail.=' Winning team must be selected';
}
$wscore=$_POST['wscore'];
if(!is_numeric($wscore)){
$haserror=true;
$errordetail.=' Winning score does not appear to be a valid number';
}
$lscore=$_POST['lscore'];
if(!is_numeric($lscore)){
$haserror=true;
$errordetail.=' Losing score does not appear to be a valid number';
}
if($lscore > $wscore){
$haserror=true;
$errordetail.=' Losing score should not be more than the winning score.';
}
if($haserror){
echo $errordetail;
exit();
}
else{
echo' Success! . Process the data here';
}
Upvotes: -1
Reputation: 1
You're trying to grab the value of the drop-down in your javascript, but then you're comparing it against the text of the drop-down. Either assign values to the default drop-down options and compare against those, or change your javascript to grab the text (e.g. "Select One") instead.
Upvotes: 0
Reputation: 8659
Your validate function is being called from the wrong place. It should be called from the form's onsubmit
method, not the submit button's onclick
method:
<form action="*********" method="post" name="form1" onsubmit="return validateMyForm()">
Upvotes: 4