Php xmlhttp.readyState无法处理mysql事务

Php xmlhttp.readyState无法处理mysql事务,php,mysql,ajax,readystate,Php,Mysql,Ajax,Readystate,我有一个ajax函数,在这里我只需调用一个php页面,该页面将运行一个mysql事务。在检查mysql日志时,我已经验证了事务是否成功运行,但xmlhttp对象跳转到else语句readyState和status 我的js代码: function promoteOptionsAllot(stid,cid,nsid,elid,flag){ anchor = document.getElementById('promoteAnchor'+elid); imgContainer = document.

我有一个ajax函数,在这里我只需调用一个php页面,该页面将运行一个mysql事务。在检查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);
}
功能升级选项分配(stid、cid、nsid、elid、标志){
anchor=document.getElementById('promoteAnchor'+elid);
imgContainer=document.getElementById('promoteStatus'+elid);
如果(标志==1){
opt1=encodeURIComponent(document.getElementById('promote_option1')。值);
opt2=encodeURIComponent(document.getElementById('promote_option2')。值);
opt3=encodeURIComponent(document.getElementById('promote_option3')。值);
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(标志==2){
params=“stid=“+encodeURIComponent(stid)+”和course=“+encodeURIComponent(cid)+”和sem=“+encodeURIComponent(nsid)+”和element=“+encodeURIComponent(elid)+”和flag=“+encodeURIComponent(flag);
}
if(window.XMLHttpRequest)
{ 
xmlhttp=新的XMLHttpRequest();
}
其他的
{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{ 
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.body.removeChild(document.getElementById('prBox');
document.body.removeChild(document.getElementById('prBackground');
结果=xmlhttp.responseText;
如果(结果=“完成”){
anchor.innerHTML=“已升级”;
imgContainer.src=“tick.png”;
}否则{
警报(“服务请求时出现问题。请重试。”);
imgContainer.src=“cross.jpg”;
}
}
否则{
imgContainer.src=“alert.gif”;
}
}
open(“POST”,“promoptallot.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send(params);
}
这是一个链接的onclick事件,在单击链接时,尽管事务成功,但imagecontainer显示alert.gif,这意味着它从未在readystate==4和status==200语句中运行代码


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

检查控制台,我在第
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');
}
它按预期工作


谢谢。

交易完成后,您是否在PHP脚本中回显“完成”
if(result==“done”)
我会尝试使用
console.log(result)
查看PHP脚本输出的内容。它输出的可能比字符串“done”要多@crush是的,我在事务完成后回显done好的,我重新阅读了你的问题,我看到你说“它从不运行readystate==4和status==200语句中的代码。”@那么您认为现在发生了什么?您是否正在使用同一个xmlhttp对象处理多个AJAX查询?您在哪里声明xmlhttp?另外,我认为您应该在if块之前输出
xmlhttp.status
xmlhttp.readyState
,以便查看它们的值。