所以我有这个函数,我希望程序等待它返回一个值而不是给出未定义的值。
function savedNumberOfLessonInDay(dayID){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById('demo4').innerHTML = this.responseText; //Returns the right number
return this.responseText;
}
};
var PageToSendTo = "AJAX/countLessonInDay.php?";
var MyVariable = dayID;
var VariablePlaceholder = "GETDayID=";
var UrlToSend = PageToSendTo + VariablePlaceholder + MyVariable;
xhttp.open("GET", UrlToSend, false);
xhttp.send();
}
<?php
require '../notWebsite/dbh.php';
session_start();
$dayID = (int)$_GET['GETDayID'];
$sqlCount = "SELECT COUNT(lessonID) FROM daylesson WHERE dayID = ?";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sqlCount)) {
header("Location: ../GymnasieArbeteHemsida.php?error=countError");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "i", $dayID);//Puts in variable in question
mysqli_stmt_execute($stmt);//Executes the question
mysqli_stmt_bind_result($stmt, $result);//Binds the reuslt of the question to the variable $result
if(mysqli_stmt_fetch($stmt)){//If the result of the question can be recieved
echo $result;//Send the result to the website
}
exit();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
?>
document.getElementById('demo').innerHTML = 'test' + savedNumberOfLessonInDay(number);
此代码将 testundefined 返回给 demo,但将 16(这是我要的数字)返回给 demo4。如何让它返回 test16 而不是 testundefined?
慕莱坞森
桃花长相依
拉丁的传说