下面是一些简单表单上的javascript验证。我第一次点击提交验证将起作用,以及消息和错误消息。如果我收到错误,请输入仍然不正确的新值并再次点击提交,验证将不再起作用,并将值传递给我的php表单。
这是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 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>
对我做错了什么的想法?
答案 0 :(得分:4)
您的验证功能是从错误的地方调用的。它应该从表单onsubmit
方法调用,而不是提交按钮的onclick
方法:
<form action="*********" method="post" name="form1" onsubmit="return validateMyForm()">
答案 1 :(得分:0)
您正试图获取javascript中下拉列表的值,但之后您将其与下拉列表中的文本进行比较。将值分配给默认下拉选项并与之进行比较,或者更改您的javascript以获取文本(例如“选择一个”)。
答案 2 :(得分:-1)
Javascript验证非常棒,但您也应该在PHP表单处理程序中执行相同的验证步骤。然后,如果它没有从PHP表单处理程序传递验证,则会再次重定向到包含表单的页面。优先预填充有效字段并注意哪些字段需要更正。
Javascript验证应被视为ADD-ON到服务器端验证。
@danl这是一些基本的PHP验证。我不确定你的gamedate变量应该用什么格式来检查它。其他是非常基本的。
$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';
}