0

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

更新课程.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>";

}

但是我遇到的问题是,如果在 jquery 中检索到来自 php 代码的错误消息,那么我不希望课程详细信息在 jquery 中进行编辑以适应当前课程文本输入以插入新的详细信息。我只希望在出现成功消息时发生这种情况。

如果出现错误消息我不希望发生任何更改,则当前课程详细信息输入和新课程详细信息输入在提交之前保持不变。

但是如何才能做到这一点呢?

4

3 回答 3

1

在更新 DOM EX 的基础上使用 JSON 响应中的任何标志:

  $.ajax({
        type: "POST",
        url: "updatecourse.php",
      dataType: 'json',
        data: $('#updateCourseForm').serialize(),
        success: function(json){
            $("#targetdiv").html(json.htmlContent);
            //Get and store the new course number and name.
        if(json.status=="success"){
 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();
}else{
//show whatever mesage u want
}

        }
     }); 
于 2012-12-31T09:35:34.037 回答
1

以 JSON 格式获取响应(这样您就可以在获得结果后操作响应......)

试试这个,

查询

$.ajax({
    type: "POST",
    url: "updatecourse.php",
    data: $('#updateCourseForm').serialize(),
    dataType:'json';  //get response as json
    success: function(result){
        if(result.errorflag){

           //do your stuff on getting error message
          var newHtml="<span style='color: red'>"+result.msg+"</span>" 
          $("#targetdiv").html(newHtml);  //i am displaying the error msg here

        }else{
           //you got success message

           var newHtml="<span style='color: green'>"+result.msg+"</span>" 
           $("#targetdiv").html(newHtml);
           //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();
         }
      }
 }); 

PHP

json_encode()将响应发送为 json.... 将响应作为带有错误标志的数组发送以检查它是成功还是错误以及要打印的味精...

...//mysqli code

    echo json_encode(array('errorflag'=>false,'msg'=>"Course details have been updated:<br/>" . $newcourseno . " - " . $newcoursename));

  }else{

  echo json_encode(array('errorflag'=>true,'msg'=>"An error has occured, Course Details have not been updated"));

}
于 2012-12-31T09:36:30.787 回答
0

在 Success Block 中这样做

success: function(html){
    var indexValue = html.indexOf("An error has occured"); 
    if (parseint(indexValue ) < 0 ) {
        return false;
    }

    $("#targetdiv").html(html);
于 2012-12-31T09:36:08.513 回答