Ajax Chained Select Boxes并不令人耳目一新

时间:2012-11-02 09:36:14

标签: php javascript ajax

好的,所以我创建了一个Javascript / PHP / Ajax应用程序。我有一系列6个选择框。第一个是用HTML硬编码的。当您更改选择时,它会通过Ajax将变量发送到PHP,后者使用该变量来查询数据库。当第二个框改变它时,然后从前面的选择框发送2个变量。

每次更改选择框时,都会添加另一个要发送的变量。最后总共有5个或6个变量,这些变量都很好,一切运行良好,几乎可以做我想做的事。

选择框3和6会出现问题。

最初他们工作正常。但是当您返回更改选择框2时,数字3不会刷新。值保持不变。与数字6相同,当我改变数字3时,框4改变并且在顺序框5中改变,但是框6保持与先前查询相同。使用的所有Ajax代码都只与变量名相同,并且相关元素已更改。

有人可以帮忙吗?我将发布链接到应用程序和相关的ajax代码。

只是你知道只有选择框1最初是可见的,其余的是由php / ajax onchange生成的。

有问题的代码

function getHTTPObject(){
 if (window.ActiveXObject) 
   return new ActiveXObject("Microsoft.XMLHTTP");
 else if (window.XMLHttpRequest) 
   return new XMLHttpRequest();
 else {
  alert("Your browser does not support AJAX.");
  return null;
 }
 }

function doWork(){ 
var selectBox = document.getElementById("SelectManufacturer"); // get  HTML select box by id
httpObject = getHTTPObject();

    httpObject.open("GET", "http://4udev.refixstudios.co.uk/wp-content/martTest/bNbAjax.php?inputText="
        +selectBox.options[selectBox.selectedIndex].text , true);

    httpObject.onreadystatechange=function()
            {
            if (httpObject.readyState==4 && httpObject.status==200)
                    {
                    document.getElementById('selectModel').innerHTML =  httpObject.responseText;;
                    }
}
httpObject.send()
}


function networkSelector()
{ 
var selectBox2 = document.getElementById("SelectManufacturer"); // get  HTML select box by id
var selectModel = document.getElementById("modelSelector");
httpObject2 = getHTTPObject();

httpObject2.open("GET", "http://4udev.refixstudios.co.uk/wp-content/martTest/NetworkAjax.php?inputText2=" +selectBox2.options[selectBox2.selectedIndex].text +"&modelText2="+selectModel.options[selectModel.selectedIndex].text, true); 

    httpObject2.onreadystatechange = function()
{

if(httpObject2.readyState == 4 && httpObject2.status==200){


  document.getElementById("networkSelect").innerHTML = httpObject2.responseText;
}


}
httpObject2.send();
}

function minuteSelector(){ 
    var selectBox3 = document.getElementById("SelectManufacturer"); // get  HTML select box by id
    var selectModel3 = document.getElementById("modelSelector");
    var NetworkSelect3 = document.getElementById("networkSelector");

    httpObject3 = getHTTPObject();

    httpObject3.open("GET", "http://4udev.refixstudios.co.uk/wp-content/martTest/minuteAjax.php?inputText3="+selectBox3.options[selectBox3.selectedIndex].text+"&modelText3="+selectModel3.options[selectModel3.selectedIndex].text+"&networkText3="+NetworkSelect3.options[NetworkSelect3.selectedIndex].text, true);

    httpObject3.onreadystatechange = function(){

if(httpObject3.readyState == 4 && httpObject3.status==200){

    document.getElementById("minuteSelect").innerHTML =httpObject3.responseText;
}


}
httpObject3.send();
}

function textSelector(){ 
    var selectBox4 = document.getElementById("SelectManufacturer"); // get  HTML select box by id
    var selectModel4 = document.getElementById("modelSelector");
    var networkSelect4 = document.getElementById("networkSelector");
    var minuteSelect4 = document.getElementById("minutesSelector");

    httpObject4 = getHTTPObject();

    httpObject4.open("GET", "http://4udev.refixstudios.co.uk/wp-content/martTest/textAjax.php?inputText4=" +selectBox4.options[selectBox4.selectedIndex].text+"&modelText4="+selectModel4.options[selectModel4.selectedIndex].text+"&networkText4="+networkSelect4.options[networkSelect4.selectedIndex].text +"&minuteText4="+minuteSelect4.options[minuteSelect4.selectedIndex].text, true);

    httpObject4.onreadystatechange = function(){

if(httpObject4.readyState == 4 && httpObject4.status==200){

    document.getElementById("textSelect").innerHTML =httpObject4.responseText;
}


}
httpObject4.send();
}

function dataSelector(){ 
    var selectBox5 = document.getElementById("SelectManufacturer"); // get  HTML select box by id
    var selectModel5 = document.getElementById("modelSelector");
    var networkSelect5 = document.getElementById("networkSelector");
    var minuteSelect5 = document.getElementById("minutesSelector");
    var textsSelector5 = document.getElementById("textsSelector");

    httpObject5 = getHTTPObjectAgainB();

    httpObject5.open("GET", "http://4udev.refixstudios.co.uk/wp-content/martTest/dataAjax.php?inputText5="+selectBox5.options[selectBox5.selectedIndex].text+"&modelText5="+selectModel5.options[selectModel5.selectedIndex].text+"&networkText5="+networkSelect5.options[networkSelect5.selectedIndex].text +"&minuteText5="+minuteSelect5.options[minuteSelect5.selectedIndex].text+"&textText5="+textsSelector5.options[textsSelector5.selectedIndex].text, true);

    httpObject5.onreadystatechange = function (){

if(httpObject5.readyState == 4){

    document.getElementById("dataSelect").innerHTML = httpObject5.responseText;
}


}
httpObject5.send()
}

以下是应用程序http://4udev.refixstudios.co.uk/csv-test/

的链接

最好的测试方法是选择任何制造商和型号然后选择网络,之后选择Apple --- The New Ipad 4G,这就是你注意到它没有改变的方式。此选择应仅将Orange作为网络

并且它称之为PHP的PHP

<?php


if (isset($_GET['inputText2'])) 
{
    $manufacturer = ($_GET['inputText2']);

    if (isset ($_GET['modelText2']))
    {

        $model = ($_GET['modelText2']);



    }

}


$conA = mysql_connect("localhost","****","*****");
if (!$conA)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("refixstu_4udev", $conA);
$selectApplea = mysql_query("SELECT DISTINCT network FROM wp_selector_apple WHERE model = '$model' ");
$selectBlackberry = mysql_query("SELECT DISTINCT network FROM wp_selector_blackberry    WHERE model = '$model'");
$selectDoro = mysql_query("SELECT DISTINCT network FROM wp_selector_doro WHERE model = '$model'");
$selectHTC = mysql_query("SELECT DISTINCT network FROM wp_selector_htc WHERE model = '$model'");
$selectLG = mysql_query("SELECT DISTINCT network FROM wp_selector_lg WHERE model = '$model'");
$selectMotorola = mysql_query("SELECT DISTINCT network FROM wp_selector_motorola WHERE model = '$model'");
$selectNokia = mysql_query("SELECT DISTINCT network FROM wp_selector_nokia WHERE model = '$model'");
$selectSamsung = mysql_query("SELECT DISTINCT network FROM wp_selector_samsung WHERE model = '$model'");
$selectSony = mysql_query("SELECT DISTINCT network FROM wp_selector_sony WHERE model = '$model'");
$selectSonyE = mysql_query("SELECT DISTINCT network FROM wp_selector_sonyericsson WHERE model = '$model'");
$selectZte = mysql_query("SELECT DISTINCT network FROM wp_selector_ZTE WHERE model = '$model'");

?>

<select id="networkSelector" style="width:100px;" align="right"  onchange="minuteSelector()"> 
<option> Select Network </option>
<?php

                        // Start Apple Testing

                        if($manufacturer == "Apple")
                        {

                                    while($rowa = mysql_fetch_array($selectApplea))

                                    { 
                                            foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }
                        else if($manufacturer == "BlackBerry")
                        {                           
                            while($rowa = mysql_fetch_array($selectBlackberry))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }
                        }
                        else if($manufacturer == "Doro")
                        {                           
                            while($rowa = mysql_fetch_array($selectDoro))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }
                        }
                        else if($manufacturer == "HTC")
                        {                           
                            while($rowa = mysql_fetch_array($selectHTC))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }
                        }
                        else if($manufacturer == "LG")
                        {

                                    while($rowa = mysql_fetch_array($selectLG))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }
                        else if($manufacturer == "Motorola")
                        {

                                    while($rowa = mysql_fetch_array($selectMotorola))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }
                        else if($manufacturer == "Nokia")
                        {

                                    while($rowa = mysql_fetch_array($selectNokia))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }
                        else if($manufacturer == "Samsung")
                        {

                                    while($rowa = mysql_fetch_array($selectSamsung))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }
                        else if($manufacturer == "Sony")
                        {

                                    while($rowa = mysql_fetch_array($selectSony))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }
                        else if($manufacturer == "Sone Ericsson")
                        {

                                    while($rowa = mysql_fetch_array($selectSonyE))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }
                        else if($manufacturer == "Zte")
                        {

                                    while($rowa = mysql_fetch_array($selectLG))

                                    { 
                                        foreach(array_unique($rowa) as $model)
                                        {
                                            echo"<option>"; echo $rowa['network']; echo"</option>";

                                        }

                                    }

                        }


                        mysql_close($conA);
                        ?>


                        </select>

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。我的onchange名为networkSelector()。我有一个名为相同的HTML表单元素,这是相互矛盾的。我已经简单地更改了函数名称,它可以在我的测试服务器上运行yay!感谢您的帮助。非常感谢