requirement
Use ajax to calculate the level in the background and output it to the PHP page. The valid input is 0 ~ 100.
design sketch
index. PHP code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
< title > judge the grade of students < / Title >
</head>
<link rel="stylesheet" />
<style>
body {
background-color: #dedede;
}
.main {
border: 7px dotted #00bcff;
width: 555px;
margin: 222px auto;
padding: 0 28px;
font-size: 28px;
}
h1 {
text-align: center;
}
input {
height: 30px;
border: none;
}
#cj {
color: red;
}
</style>
<body>
<div class="main">
<h1>Student grade</h1>
<p>Student Name: < input type = "text" placeholder = "please enter name" id = "name" / ></p>
<p>
Student score: < input type = "text" placeholder = "please enter grade" id = "num" onchange = "zl()" / >
</p>
<p>Student achievement: < span id = "CJ" > < / span ></p>
</div>
<script>
function zl() {
var num = document.getElementById("num").value;
//1. create object
var xmlhttp;
if (window.XMLHttpRequest) {
//IE7 +, Firefox, chrome, opera, Safari browser execution code
xmlhttp = new XMLHttpRequest();
} else {
//IE6, IE5 browser execution code
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//2. Determine whether the object is ready
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("cj").innerHTML = xmlhttp.responseText;
}
};
//3. Make a request
xmlhttp.open("GET", "data.php?num=" + num, true);
xmlhttp.send();
}
</script>
</body>
</html>
data. PHP code
<?php
$num = $_GET["num"];
switch (intval($num / 10)) {
case 10:
$str = "A";
break;
case 9:
$str = "A";
break;
case 8:
$str = "B";
break;
case 7:
$str = "C";
break;
case 6:
$str = "D";
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
$str = "E";
break;
default:
$STR = "please enter the correct score";
}
Echo "$STR level";
?>