xmlhttp.readyState无法使用mysql事务

时间:2013-01-11 13:35:32

标签: php mysql ajax readystate

我有一个ajax函数,我只是调用一个运行mysql事务的php页面。在检查mysql日志时,我已经验证事务成功运行但是xmlhttp对象跳转到else语句readyState和status。

我的js代码:

function promoteOptionsAllot(stid,cid,nsid,elid,flag){
anchor = document.getElementById('promoteAnchor'+elid);
imgContainer = document.getElementById('promoteStatus'+elid);
if(flag == 1){
opt1 = encodeURIComponent(document.getElementById('promote_option1').value);
opt2 = encodeURIComponent(document.getElementById('promote_option2').value);
opt3 = encodeURIComponent(document.getElementById('promote_option3').value);
opt4 = encodeURIComponent(document.getElementById('promote_option4').value);
params =   "stid="+encodeURIComponent(stid)+"&course="+encodeURIComponent(cid)+"&sem="+encodeURIComponent(nsid)+"&element="+encodeURIComponent(elid)+"&popt1="+opt1+"&popt2="+opt2+"&popt3="+opt3+" &popt4="+opt4+"&flag="+encodeURIComponent(flag);
 }
else if(flag == 2){
params =   "stid="+encodeURIComponent(stid)+"&course="+encodeURIComponent(cid)+"&sem="+encodeURIComponent(nsid)+"&element="+encodeURIComponent(elid)+"&flag="+encodeURIComponent(flag);
}


if (window.XMLHttpRequest)
 { 
 xmlhttp=new XMLHttpRequest();
 }
else
 {
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  { 
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
document.body.removeChild(document.getElementById('prBox'));
document.body.removeChild(document.getElementById('prBackground'));
result = xmlhttp.responseText;  
if(result == "done"){
anchor.innerHTML = "<span style='color:#198D19;'><b>Promoted</b></span>";
imgContainer.src = "tick.png";
}else {
alert("There was a problem serving the request. Please try again.");
imgContainer.src = "cross.jpg";
}
 }
else{
imgContainer.src = "alert.gif";
}
 }
xmlhttp.open("POST","promoptallot.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);
}

它是链接的onclick事件,并且在单击链接时虽然事务成功但不知何故imagecontainer显示alert.gif,这意味着它永远不会在readystate == 4和status == 200语句中运行代码。

请不要建议使用jquery。我知道它是一个稳定的框架和其他东西,但我只需要一个答案。

1 个答案:

答案 0 :(得分:0)

检查控制台,我在第document.body.removeChild(document.getElementById('prBox')); document.body.removeChild(document.getElementById('prBackground'));行找到了问题

这些行必须以案例为基础执行,我没有把它放在if语句中。

现在把它像if (flag ==1){ document.body.removeChild(document.getElementById('prBox')); document.body.removeChild(document.getElementById('prBackground')); }一样按预期工作。

感谢。