duan0414 2011-05-08 01:33
浏览 9
已采纳

为什么这些功能中的一个只能一次工作?

Even if I remove the if statements, only one of these following will work at one time. To get the former to work, I have to comment out the latter.

<?
if(isset($_POST['region'])){
echo "<script> showRecords('".$_POST['region']."','region','country') </script>";}
if(isset($_POST['country'])){
echo "<script> showRecords('".$_POST['country']."','country','provice') </script>";}
?>

The script is referring to this:

function showRecords(str,column,nextDiv)
{
if (str=="")
  {

  document.getElementById(nextDiv).innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    document.getElementById(nextDiv).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get"+column+".php?"+column+"="+str,true);
xmlhttp.send();
}

The script leads to a very simple set of pages where that list some values based on some $_GET information.

I just cannot understand why it is only allowing me to do one of these scripts at a time. I even tried cloning the function to showRecords2, and it will still only do showRecords or showRecords2.

  • 写回答

1条回答 默认 最新

  • dougu1990 2011-05-08 02:06
    关注

    Replace xmlhttp=new XMLHttpRequest() with var xmlhttp=new XMLHttpRequest(). Notice the var keyword added. What happened is xmlhttp is becoming a global scope variable and it gets overwritten with new values/argument/parameters everytime you make a request e.g. calling showRecords twice while the first one is still doing stuff the second call overwrites it.

    Remember to make all your variables in the function level to avoid overwrites unless they are actually gonna be used in the global scope. It's time consuming to debug this kind of issues especially when you don't know where to find stuff. Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架