Php 如何让数据允许小数
我制作了一个程序,将6个等级输入表格,然后计算字母等级和平均值。我唯一的问题是我不知道如何让表单接受小数。我尝试更改我的filter\u输入语句来清理浮动,但这似乎无法解决问题。如有任何想法,将不胜感激Php 如何让数据允许小数,php,html,Php,Html,我制作了一个程序,将6个等级输入表格,然后计算字母等级和平均值。我唯一的问题是我不知道如何让表单接受小数。我尝试更改我的filter\u输入语句来清理浮动,但这似乎无法解决问题。如有任何想法,将不胜感激 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" cont
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grades</title>
<link rel="stylesheet" href="bootstrap.min.css">
<script>
window.onload = () => {
document.getElementById('quiz1').focus();
document.getElementById('quiz1').select();
}
</script>
<style>
input[type="text"] {width: 4em !important; text-align: right; margin-left: .5em; }
ul { list-style-type: none; padding-left: 0; }
</style>
</head>
<body>
<div class="container">
<div class="col-lg-6 mx-auto text-center">
<h1>Enter Quiz Grades</h1>
<form action="." method="post">
<?php
// declare an array to store bowling scores
$quizScores = array();
// declare a variable to be used for the natural counting of the number of games
$numberOfScores = 6;
// declare a variable to be used for array elements - subtract 1 since array elements start at 0
$gamesArrayElements = $numberOfScores - 1;
for ($i = 0; $i <= $gamesArrayElements; $i++){
$quizNumber = $i + 1;
// load values from POST into array elements
if (strlen($_POST['quiz' . $quizNumber]) > 0){
$quizScores[$i] = filter_input(INPUT_POST, 'quiz' . $quizNumber, FILTER_SANITIZE_NUMBER_FLOAT);
}
echo "<div class='form-group form-inline justify-content-center'>\n";
echo "<label for='quiz$quizNumber'>Quiz $quizNumber: </label>\n";
echo "<input type='text' class='form-control' name='quiz$quizNumber' id='quiz$quizNumber' value='$quizScores[$i]'>%\n";
echo "</div>\n";
}
?>
<div class="form-group">
<input type="submit" class="btn btn-secondary" value="Find Average of <?php echo $numberOfScores; ?> Highest Quiz Grades">
<a href="." class="btn btn-secondary">Clear</a>
</div>
</form>
<?php
if (count($quizScores) === $numberOfScores){
$total = 0;
echo "<h2>Scores:</h2>\n";
echo "<ul>\n";
// for loop to print array elements and add each score to the total
for ($i = 0; $i <= $gamesArrayElements; $i++){
echo "<li>$quizScores[$i] " . getLetterGrade($quizScores[$i]) . "</li>\n";
$total += $quizScores[$i];
}
echo "</ul>\n";
$average = $total / $numberOfScores;
echo "<h2>Average Score:</h2>\n";
echo "<p>" . number_format($average, 1, .2) . " - " . getLetterGrade($average) . "</p>\n";
} else {
if(count($quizScores) > 0) {
echo "<h2 class='text-danger'>All textboxes should contain grades.</h2>\n";
}
}
?>
</div>
</div>
</body>
</html> ```
分数
window.onload=()=>{
document.getElementById('quiz1').focus();
document.getElementById('quiz1').select();
}
输入[type=“text”]{宽度:4em!重要;文本对齐:右;边距左:.5em;}
ul{列表样式类型:无;左侧填充:0;}
输入测验分数
我将number\u格式($average,1.2)
更改为number\u格式($average,1)
,然后我将平均值四舍五入到小数点后一位。我还删除了getLetterGrade()
函数,因为它没有在代码中定义。最后,我将表单操作更改为“
,而不是”
,因为它在我的测试环境中不起作用
也许你需要?这样你就可以看到你的代码出了什么问题