Ok so i have created a Javascript/PHP/Ajax application. I have a sequence of 6 select boxes. The first is hardcoded in HTML. When you change the selection it sends a variable via Ajax to PHP which uses the variable to query the database. When the second box changes it then sends the 2 variables from the previous select boxes.

Each time a select box is changes it adds another variable to send. In the end there is a total of 5 or 6 variables which is all good, everything is functioning well and almost doing what i want it to do.

My problems occur with select boxes 3 and 6.

Initially they work fine. But when you go back to change select box 2, number 3 does not refresh. The values stay the same as before. The same with number 6, when i change number 3, box 4 changes and in sequence box 5 changes, however box 6 remains the same as the previous query. All Ajax code used is identical with just variable names and relevant Elements changed.

Can anybody help? i will post the link to the application and the ajax code in question.

Just so you know only select box 1 is visible initially, the rest are generated by php/ajax onchange.

Heres the code in question

// Get the HTTP Object


// Get the HTTP Object

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

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

    if (httpObject2 != null) {
        httpObject2.open("GET", "http://somesite.co.uk/NetworkAjax.php?inputText2="
        +selectBox2.options[selectBox2.selectedIndex].text +"&modelText2="+selectModel.options[selectModel.selectedIndex].text , true);
        httpObject2.send(null); 
        httpObject2.onreadystatechange = setOutput2;
    }
}
// Change the value of the outputText field
function setOutput2(){
      if(httpObject2.readyState == 4){

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

}
var httpObject2 = null;

Here is the link to the application http://4udev.refixstudios.co.uk/csv-test/

The best way to test is buy selecting any manufacturer and model then network, after that select Apple---The New Ipad 4G, this is how you notice that it does not change. This selection should only bring back Orange as network

And Heres the PHP that it calls

<?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>

At first glance, it would seem that your variables are not being cleared on refresh of the initial object.

When you change "SelectManufacturer" you need to either clear the variables of the remaining values of your selector boxes. Since the page doesn't refresh, the variables are remaining set.

You may want to refactor your code, and put in a place a system that checks heirarchy of selection. When any of the previous (or, ones that can actually affect change in the children selectors) are changed, then you need to clear out or remove from the DOM the selections you don't want anymore.

Lastly, you are pulling a TON of queries on your database. Lets say you have 5000 people connected at the same time, and 5000 are configuring their devices, that's upwards of 5000 queries per second that will bog the heck out of your stream causing lag, ques, and other bad things to happen. You're not dealing with a TON of data here. It would probably be better to have the initial selection pull into memory all of the options available in one pull, then you don't have to keep polling the server each time a selection is made - only on the important ones (in this case, one). This will also help to reset the other selection options and get rid of your initial problem, while simultaniously reducing server load.

Hi ryantroop,

Thanks for the input this has been driving me insane for days. I will look at implementing the heirarchy, that sounds like the way forward for this project. My only question with that is, not all of the select boxes are present at the time the page loads. Will that cause issues when coding it. I'm thinking that a simple javascript if statement will be able to solve that problem, maybe to check if the select box is present on the page?

With regards to the database requests. Would you recomend doing the final SQL Query at the begining.

So the SQL would read, SELECT * FROM 'AppleTable'

and then store it in a mutlti dimensional array? Then rather than sending the query to the database, PHP can just query the array?

i have solved the problem. My onchange called networkSelector(). I had a HTML form element named the same and that was conflicting. I have simple changed the function name and it works on my test server yay! thank you for the help. Muchly appreciated and i am going to take a further look into storing the SQL results local, however server resource is not an issue. I have designed this for a big company. thank you for this guidance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.