gpt4 book ai didi

php - 为什么一次只能使用其中一个功能?

转载 作者:行者123 更新时间:2023-11-30 09:04:56 26 4
gpt4 key购买 nike

即使我删除了 if 语句,一次也只能使用以下其中一个。为了让前者起作用,我必须注释掉后者。

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

脚本是指这个:

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();
}

该脚本指向一组非常简单的页面,其中列出了一些基于某些 $_GET 信息的值。

我只是不明白为什么它一次只允许我执行其中一个脚本。我什至尝试将函数克隆到 showRecords2,它仍然只会执行 showRecords 或 showRecords2。

最佳答案

xmlhttp=new XMLHttpRequest() 替换为 var xmlhttp=new XMLHttpRequest()。注意添加的 var 关键字。发生的事情是 xmlhttp 正在成为一个全局范围变量,并且每次您发出请求时它都会被新值/参数/参数覆盖,例如调用 showRecords 两次,而第一个仍在执行操作,第二个调用将覆盖它。

请记住在函数级别创建所有变量以避免覆盖,除非它们实际上将在全局范围内使用。调试此类问题非常耗时,尤其是当您不知道在哪里可以找到东西时。希望对您有所帮助!

关于php - 为什么一次只能使用其中一个功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5925094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com