我在让表单验证正常工作时遇到了一些麻烦。我正在使用 PHP,我认为这与我使用 $_POST 访问表单的方式有关。我正在使用该操作并将其附加到单独的页面。
HTML
<html>
<head>
<title>Grade Calculator</title>
</head>
<body>
<form name="myForm" method="post" id="myForm" action="processForm.php" >
<table width="400" border="1" cellspacing="4" cellpadding="4">
<tbody><tr>
<td>Assignments:</td>
<td><input type="text" name="assGrade" id="assGrade" value="0" /></td>
</tr>
<tr>
<td>Mid Term: </td>
<td><input type="text" name="midGrade" id="midGrade" value="0" /></td>
</tr>
<tr>
<td>Final: </td>
<td><input type="text" name="finGrade" id="finGrade" value="0" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnSubmit" id="btnSubmit" value="Calculate" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
php`
<?php
$assGrade = $_POST[document.myForm.assGrade.value];
$midGrade = $_POST[document.myForm.midGrade.value];
$finGrade = $_POST[document.myForm.finGrade.value];
$avg = ($assGrade + $midGrade + $finGrade) / 3;
$grade = '';
switch($avg){
case($avg >=90):
$grade = 'A';
break;
case($avg >=80):
$grade ='B';
break;
case($avg >=70):
$grade ='C';
break;
case($avg >=60):
$grade ='D';
break;
default;
$grade ='F';
};
echo "Your average is ".$avg.'<br/>';
echo "Your letter Grade is " .$grade;
?>