下面我有一個帶有ajax代碼的jquery,它將顯示更改課程詳細信息的值,方法是插入一組輸入中的新數據(輸入集合用戶已經做出改變)到另一組輸入(輸入組,其中指出課程的當前細節)。另外,課程的下拉菜單進行更改以適應新的變化,如果它需要改變:如果出現錯誤消息,不希望將值插入到文本輸入
function submitform() {
$.ajax({
type: "POST",
url: "updatecourse.php",
data: $('#updateCourseForm').serialize(),
success: function(html){
$("#targetdiv").html(html);
//Get and store the new course number and name.
var newCourseNo = jQuery("#newCourseNo").val();
var newCourseName = jQuery("#newCourseName").val();
var newDuration = jQuery("#newDuration").val();
//Set your current course number and name to your number and name.
jQuery("#currentCourseNo").val(newCourseNo);
jQuery("#currentCourseName").val(newCourseName);
jQuery("#currentDuration").val(newDuration);
//Find the currently selected course and update it.
var selectedOption = jQuery("#coursesDrop option:selected");
var label = selectedOption.text().split(" - ");
selectedOption.text(newCourseNo + " - " + newCourseName);
$('#targetdiv').show();
}
});
}
現在$("#targetdiv")
是它顯示它是通過AJAX訪問從PHP頁面中檢索到的成功或錯誤消息ID:
updatecourse.php:
...//mysqli code
echo "<span style='color: green'>Course details have been updated:<br/>" . $newcourseno . " - " . $newcoursename . "</span>";
}else{
echo "<span style='color: red'>An error has occured, Course Details have not been updated</span>";
}
但我遇到的問題是,如果從PHP代碼錯誤消息在jQuery是檢索,然後我不希望課程內容,使編輯的jquery來容納當前課程文本輸入以插入新的細節。如果成功消息出現,我只希望發生這種情況。
如果出現錯誤消息,我不希望發生變化,當前課程詳細信息輸入和新課程詳細信息輸入在提交之前保持不變。
但是,這怎麼能實現呢?
我會從PHP返回一個json對象,包括任何消息和狀態,無論它是否正常。如果出現錯誤,請不要做任何更新,如果沒有,請繼續並進行更新。 – kennypu