嗨所以我尝试创建一个BMI计算器,但由于某些原因它无法正常工作。我是php / ajax的总菜鸟,所以如果有人知道我做错了,请告诉我! :d
gewicht =体重 lengte = length
AJAX代码:
nextInLine(testArr.slice(), 6)
PHP代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ajax</title>
</head>
<body>
<script>
function ajax(gewicht,lengte) {
let debug = true;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
let controlScript = "BMI.php";
let httpString = controlScript + "?gewicht=" + gewicht + "&lengte_cm" + lengte;
let httpResponse = "";
if(debug) console.log(httpString);
xmlhttp.open("GET", httpString, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if(debug) console.log("http response = " + xmlhttp.responseText);
httpResponse = xmlhttp.responseText;
document.getElementbyId('resultaat').innerhtml = httpRespsonse;
}
}
}
</script>
<form action="BMI.php" method="post">
<input name="gewicht" id="gewicht" type="text" onkeyup="ajax(gewicht.value)" placeholder="gewicht"><br/>
<input name="lengte" id="lengte" type="text" onkeyup="ajax(lengte.value)" placeholder="lengte">
<input type="submit" value="submit">
</form>
<div id="resultaat"></div>
</body>
</html>
答案 0 :(得分:0)
您没有将这两个参数发送到文本框的keyup事件的ajax函数,因此它无法正常工作。你可以这样做:
<input name="gewicht" id="gewicht" type="text" placeholder="gewicht" /><br/>
<input name="lengte" id="lengte" type="text" placeholder="lengte" />
<input type="button" value="submit" onClick="ajax(gewicht.value,lengte.value)"/>