如果出现错误消息,则不希望将值插入文本输入

时间:2012-12-31 09:20:48

标签: php jquery ajax

下面我有一个带有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")是显示从php页面检索的成功或错误消息的id,可通过ajax访问:

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>";

}

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

如果出现错误消息我想要不进行任何更改,当前课程详细信息输入和新课程详细信息输入在提交之前保持不变。

但是如何实现呢?

3 个答案:

答案 0 :(得分:1)

根据更新DOM,在JSON响应中使用任何标志 EX:

  $.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
}

        }
     }); 

答案 1 :(得分:1)

以JSON格式获取响应(以便在获得结果后可以操作响应...)

试试这个,

<强> JQUERY

$.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"));

}

答案 2 :(得分:0)

在成功阻止中这样做

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

    $("#targetdiv").html(html);