Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript调用函数有问题吗?_Javascript_Php_Ajax_Xml - Fatal编程技术网

Javascript调用函数有问题吗?

Javascript调用函数有问题吗?,javascript,php,ajax,xml,Javascript,Php,Ajax,Xml,我正在编写一个工具,从SQL数据库获取数据,将查询结果(一个数字)转换为传递给javascript变量的颜色 简而言之,我使用ajax打开php代码来查询数据库并返回它 我一直面临的问题是,我无法让javascript像inteded一样工作。为了调试的目的,我在一些窗口中添加了警报,以了解发生了什么 因此,我希望代码的运行方式是: PickColor由“Try it”按钮调用,并将“B03”传递为 arg1 getCount在PickColor内部调用并发送xml 请求,并在从 xmlhttp

我正在编写一个工具,从SQL数据库获取数据,将查询结果(一个数字)转换为传递给javascript变量的颜色

简而言之,我使用ajax打开php代码来查询数据库并返回它

我一直面临的问题是,我无法让javascript像inteded一样工作。为了调试的目的,我在一些窗口中添加了警报,以了解发生了什么

因此,我希望代码的运行方式是:

  • PickColor由“Try it”按钮调用,并将“B03”传递为 arg1
  • getCount在PickColor内部调用并发送xml 请求,并在从 xmlhttp.responseText字符串
  • 它将此数字返回到PickColor中的变量temp
  • 一系列if/else语句选择颜色
  • 但从我的代码运行方式来看:

  • pickColor被正确调用
  • 窗口警报按如下顺序出现:window.alert(arg1), 窗口警报(温度)、窗口警报(颜色)、窗口警报(计数)。我 本以为是窗口。警报(计数)在 函数,它肯定会被第二行调用 最后,由于这一点,它没有返回准备就绪的计数 窗口。警报(临时)
  • 我知道我可以把这两个函数合并成一个函数来工作,但是我很好奇为什么现在不行

    代码如下:

    <html>
    <body>
    <script>
    
    var count;
    var temp;
    
    function getCount(arg1) {
    
        if (arg1 == "") 
        {
            document.getElementById("Count").innerHTML = "";
            return;
        }
        else 
        { 
            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) {
                    var temp = xmlhttp.responseText; //This returns the <doctype HTML>
    
                    var test = temp.split("<body>"); //Splitting the string into an array so that the number is the first in the 2nd array
    
                    count = parseInt(test[1]); //Grabbing the Int and setting it back to count
    
                    window.alert(count); //shows 192 correctly. Runs 4th
    
                    return count; //returning count to variable temp in the 'pickColour' function
                }
            }
    
           xmlhttp.open("GET","getdata.php?q="+arg1,true);
           xmlhttp.send();
    
        }
    }
    
    function pickColour(arg1)
    {
        window.alert(arg1); //Runs first
        var temp = getCount(arg1); 
        window.alert(temp); //shows as undefined... - Runs 2nd
    
        var colour;
    
        if (temp <= 200 &&  temp >= 100)
        {
            color = '#0f70d2';
        }
        else if (temp < 100)
        {
            color = '#3bff00';
        }
        else if (temp <= 400 && temp >= 500)
        {
            color = '#ff0000';
        }
        else
        {
            color = '#000000';
        }
    
        window.alert(color); //Runs 3rd
        return color;
    }
    
    
    
    </script>
    
    
    <form>
    <select name="buildings" onchange="getCount(this.value)">
      <option value="">Select a building:</option>
      <option value="B01">Building 1</option>
      <option value="B22">Building 22</option>
      <option value="B03">Building 3</option>
      <option value="B44">Building 44</option>
      </select>
    </form>
    <br>
    <div id="Count"><b></b></div>
    
    <button onclick=pickColour("B03")>Try it</button>
    
    <p id="demo"></p>
    
    
    </body>
    </html>
    
    
    var计数;
    无功温度;
    函数getCount(arg1){
    如果(arg1==“”)
    {
    document.getElementById(“Count”).innerHTML=“”;
    返回;
    }
    其他的
    { 
    if(window.XMLHttpRequest)
    {
    //IE7+、Firefox、Chrome、Opera、Safari的代码
    xmlhttp=新的XMLHttpRequest();
    } 
    其他的
    {
    //IE6、IE5的代码
    xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
    }
    xmlhttp.onreadystatechange=函数()
    {
    if(xmlhttp.readyState==4&&xmlhttp.status==200){
    var temp=xmlhttp.responseText;//这将返回
    var test=temp.split(“”;//将字符串拆分为一个数组,使数字位于第二个数组的第一个
    count=parseInt(测试[1]);//获取Int并将其设置回count
    window.alert(count);//正确显示192。运行4次
    return count;//将计数返回到'pickColor'函数中的变量temp
    }
    }
    open(“GET”、“getdata.php?q=“+arg1,true”);
    xmlhttp.send();
    }
    }
    功能选择器颜色(arg1)
    {
    window.alert(arg1);//首先运行
    var temp=getCount(arg1);
    window.alert(temp);//显示为未定义…-第二次运行
    颜色变异;
    如果(温度=100)
    {
    颜色='#0f70d2';
    }
    否则,如果(温度<100)
    {
    颜色='3bff00';
    }
    否则,如果(温度=500)
    {
    颜色='#ff0000';
    }
    其他的
    {
    颜色='000000';
    }
    window.alert(颜色);//第三次运行
    返回颜色;
    }
    选择一个建筑:
    1号楼
    22号楼
    3号楼
    44号楼
    
    试试看


    非常感谢,
    xmlhttp.open
    的第三个参数是值。由于代码将其设置为true,因此请求是异步的:
    getCount
    启动xmlHttpRequest操作,但在完成之前返回。只有当操作完成时(一段时间后),才会调用xmlhttp.onreadystatechange`并生成计数警报

    正如Jaromanda X所提到的,AJAX/XMLHttpRequest调用是异步的,因此您必须稍微修改代码


    代码检查返回计数的范围并确定颜色(需要将
    if…else if
    块)放入
    if(xmlhttp.readyState==4&&xmlhttp.status==200)
    块中。

    XMLHttpRequest是异步的,因此,结果将在未确定的时间之后才能得到。啊,这是有意义的(这是我第一次使用AJAX),我知道我会看到我能做什么。谢谢你,这肯定有助于解释发生了什么!没问题。如果可以帮助其他寻求解决方案的人,请将其中一个答案标记为正确答案。:)为解释这一切是如何运作的而干杯,我应该在手边读一读这一切到底是如何运作的!非常感谢。