표시되는 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);
}
텍스트 변경하지만이 없어 몇 초 후.
더 많은 코드를 추가했습니다. location.reload()를 제거했습니다. 지금 가지고있는 문제는 PHP 서버의 응답을 검증 할 수 없다는 것입니다. MYSQL 쿼리를 실행하지 못하면 메시지를 표시하려고합니다. – Simon
PHP 코드의 SQL 쿼리가 실패하면 오류 메시지가 포함 된 문자열을 반환하거나 -1을 반환 할 수 있습니다. – Sav