开发者

How come only one of these functions will work at a single time?

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;
    }
开发者_C百科  }
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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜