下面我有一个带有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