当我在html页面中直接使用action="updatebonpen.php"
时,它会很好地更新数据库。现在,我尝试使用jQuery更新我的数据库,它只是继续显示“添加,请稍候......”而不更新数据库。
我之前在其他网站上使用过相同的代码,并且在那里工作得非常好。以下是代码:
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Adding, please wait..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var bonpen= encodeURI(document.getElementById('bonpen').value);
var points= encodeURI(document.getElementById('points').value);
var reason= encodeURI(document.getElementById('reason').value);
var comment = encodeURI(document.getElementById('comment').value);
var username = encodeURI(document.getElementById('username').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'updatebonpen.php?bonpen=' +bonpen+'&points=' +points+'&reason=' +reason+'&comment=' +comment+'&username=' +username+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'Ok.'+response;
}
}