2012-12-31 52 views
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:

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来容纳当前课程文本输入以插入新的细节。如果成功消息出现,我只希望发生这种情况。

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

但是,这怎么能实现呢?

+0

我会从PHP返回一个json对象,包括任何消息和状态,无论它是否正常。如果出现错误,请不要做任何更新,如果没有,请继续并进行更新。 – kennypu

回答

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

} 
+0

我可以很快问,如果有错误,我只是不想改变任何东西,如果我只是包括一个空的其他东西,页面提交之前保持不变?换句话说,没有任何价值观会从他们的投入中转移? – user1830984

+0

是的..因为它是一个AJAX函数..页面没有刷新..所以它应该保持与提交之前相同...您可以显示错误信息,因为我已经在错误条件下成功完成了... – bipen

+0

+1和最好的答案,谢谢:) – user1830984

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 
} 

     } 
    }); 
+0

upvoted答案,这将工作 – user1830984

0

做像这样成功阻止

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

    $("#targetdiv").html(html); 
相关问题