2012-12-31 49 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); 
相關問題