所以我制作了一個關于考試的程序,我需要確定學生的字母成績,但首先他們必須回答一些問題,然后根據學生得到的分數,學生將獲得字母成績,有人推薦我使用 AJAX 因為我需要在表單中完成該過程而不刷新,所以我嘗試制作它,但我最終感到困惑,有人可以幫助我嗎?問題是結果總是“沒有給出分數”,但我在控制臺中嘗試過,沒有錯誤,結果是正確的結果。這是代碼:<div class = "form"><button id = "submit" name = "submit" value = "submit" > SUBMIT </button><div id="myModal" class="modal"> <div class="modal-content"> <h3> Are you sure you want to submit? </h3> <button id = "yes" name = "yes" class = "yes" value = "submit"> YES </button> <button id = "no" name = "no" class = "no"> NO </button> </div></div><div id="myModalLast" class="modalLast"> <div class="modal-contentLast"> <a href = "personal.php"> <span class="close">×</span> </a><div class = "pic"></div> <h3> Full name: Cathleen Joyce Imperial Almeda </h3> <h3> Total items:20 <p id = "scoress" name = "scorename"></p> </h3> <h1> <br><p id = "scores" name = "realscores"></p> Rank:<p id = "rank"></p></h1> </div></div></div>這是針對 ajax 部分的: <script>function loadDoc(scoress) { var xhttp = new XMLHttpRequest(); xhttp.open("POST", "examExtension.php", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { scoreElement.innerHTML = "Your Score: " + this.responseText; console.log('Grade calculation complete'); } }xhttp.send(scoress);}</script>這是 examExtension.php <?php// Check if the score is given. If it is, continue. Otherwise stop the script.if (!isset($_POST['scoress'])) { echo 'No score has been given'; exit;}// Convert score value to a number.$score = intval($_POST['scoress']);if ($score > 19 and $score < 21) { echo "A+";} if ($score > 18 and $score < 20) { echo "A";} if($score > 17 and $score < 19) { echo "A-";}if ($score > 16 and $score < 18) { echo "B+";} if ($score > 15 and $score < 17) { echo "B";} if ($score > 14 and $score < 16) { echo "B-";} if ($score > 13 and $score < 15) { echo "C+";}
1 回答

白板的微信
TA貢獻1883條經驗 獲得超3個贊
在xhttp.send()
您忘記設置參數的變量。根據w3schools Ajax post 請求,您必須像這樣發布數據:
function loadDoc(scoress) {
? ? var xhttp = new XMLHttpRequest();
? ? xhttp.onreadystatechange = function() {
? ? ? ? if (this.readyState == 4 && this.status == 200) {
? ? ? ? ? ? scoreElement.innerHTML = "Your Score: " + this.responseText;
? ? ? ? ? ? console.log('Grade calculation complete');
? ? ? ? }
? ? }
? ? let data = "scoress=" + scoress;
? ? xhttp.open("POST", "examExtension.php", true);
? ? xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
? ? xhttp.send(data);
}
那應該對你有用!
- 1 回答
- 0 關注
- 147 瀏覽
添加回答
舉報
0/150
提交
取消