2014-04-20 1 views
0

표시되는 MYSQL 데이터베이스의 제품이 포함 된 카테고리를 삭제하려고 시도 할 때이 코드를 사용하여 사용자에게 메시지를 표시하고 있습니다. html로 웹 사이트를 통해 :몇 초 후 텍스트가 변경됩니다. http.responseText, .innerHTML (자바 스크립트, Ajax)

function deleteCategory() 
{ 
var http = new XMLHttpRequest(); 
var url = "/637415/admin/scripts/deleteCategory.php"; 
var params = "selectCatDelete=" + document.getElementById("selectedCatDelete").value; 
console.log("The value is:", params); 
http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 
http.onreadystatechange = function() {//Call a function when the state changes. 
    if(http.responseText == 0 && http.status == 200) { 
     document.getElementById("responseText").innerHTML = "You need to delete products from the category, go to the delete products page here: <a href='/637415/admin/deleteProduct.php'>Delete produy</a>"; 
    } 
    if(http.readyState == 4 && http.status == 200) { 
     document.getElementById("responseText").innerHTML = "Category deleted"; 
    } 

} 
http.send(params); 
console.log(params); 

}

텍스트 변경하지만이 없어 몇 초 후.

답변

0

나머지 코드를 알지 못하면 해당 location.reload()와 관련 될 수 있습니다. 페이지를 새로 고침하자마자 메시지를 포함하여 자바 스크립트를 사용하여 변경 한 모든 내용이 손실됩니다.

+0

더 많은 코드를 추가했습니다. location.reload()를 제거했습니다. 지금 가지고있는 문제는 PHP 서버의 응답을 검증 할 수 없다는 것입니다. MYSQL 쿼리를 실행하지 못하면 메시지를 표시하려고합니다. – Simon

+0

PHP 코드의 SQL 쿼리가 실패하면 오류 메시지가 포함 된 문자열을 반환하거나 -1을 반환 할 수 있습니다. – Sav

0

요청이 완료된 후 왜 메시지를 표시하지 않습니까? 작동 여부 확인 :

http.onreadystatechange = function() {//Call a function when the state changes. 
    //if there is a request not initialized error: 

    //Request finished and response is ready 
    if(http.readyState == 4 && http.status == 200) { 
     if(http.responseText == 0) { 
     document.getElementById("responseText").innerHTML = "You need to delete 
     products from the category first, go to the delete products page here: 
     <a href='/637415/admin/deleteProduct.php'>Delete Products</a>"; 
     }else 

     location.reload(); 

     } 
} 
+0

ReferenceError :이 코드를 시도 할 때 deleteCategory가 정의되어 있지 않습니다. 도움이된다면 초기 게시물에 더 많은 코드를 추가했습니다. – Simon

관련 문제