0

I'm trying to use AJAX and PHP to update the options of a drop down menu based on the previous selection from another drop down menu.

I have this script in the head of my document:

<script>
function show_districts(str)
{
if (str.length==0)
  {
  document.getElementById("districts_dropdown").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("districts_dropdown").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_districts.php?q="+str,true);
xmlhttp.send();
}
</script>

... which, when onchange() is triggered, passes the value of this drop down:

<label for="banner" class="medium">Banner</label>
<select name="banner" id="banner" class="textbox short_field" onchange="show_districts(this.value)">
<option value=""></option>
<?php

    $result = mysql_query("SELECT name FROM banners", $connection);
    if (!result) {
        die("Database query failed: " . mysql_error());
    }

    while ($row = mysql_fetch_array($result)) {
        echo '<option value="' . $row["name"] . '"';
        if($row["name"] == $banner) { echo 'selected';} ;
        echo '>' . $row["name"] . '</option>';
    }
?>
</select>

... to this php file:

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
$q = $_REQUEST['q'];

if($q = 'TBOOTH') {$id = 2;}

$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = '".$id."' GROUP BY dist.id ORDER BY dist.id ASC", $connection);
    if (!result) {
        die("Database query failed: " . mysql_error());
    }

    while ($row = mysql_fetch_array($result)) {
        echo '<option value="' . $row["name"] . '"';
        if($row["name"] == $district) { echo ' selected';} ;
        echo '>' . $row["name"] . '</option>';
    }

mysqli_close($connection);
?>

... which should return the values from the php file to this drop down menu with <span id="districts_dropdown">:

<label for="district" class="medium">District</label>
<select name="district" class="textbox short_field">
<option value=""></option>
<span id="districts_dropdown"></span>
</select>

This works nicely if <span id="districts_dropdown"> is outside of the <select> tags, but not within. Any insight as to why would be greatly appreciated.


Thanks for you help Popnoodles! I fixed part of the problem by having the php code generate the entire <select> tag like this:

<?php
$q = $_REQUEST['q'];
$id = "";

if ($q = 'TBOOTH') {
    $id = "2";
} else {
    if ($q = 'WIRELESS ETC') {
        $id = "3";
    }
}

echo '<select name="district" class="textbox short_field">
<option value=""></option>';

$query = "SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = '".$id."' GROUP BY dist.id ORDER BY dist.id ASC";

$result = mysql_query($query, $connection);
    if (!result) {
        die("Database query failed: " . mysql_error());
    }

    while ($row = mysql_fetch_array($result)) {
        echo '<option value="' . $row["name"] . '"';
        if($row["name"] == $district) { echo ' selected';} ;
        echo '>' . $row["name"] . '</option>';
    }

mysql_close($connection);
?>

But now the $id variable I have at the top of that code isn't updating when the onchange() is triggered.

7
  • 1
    Inside a select should be option or optgroup. You can't stick a span in there. Why are you doing that? Commented Jan 10, 2014 at 1:23
  • Because the php code should return the option tags with the dynamically generated options. I tested this by writing in manually the options I need within a span inside select and it functioned normally, but for some reason php can't do the same. Commented Jan 10, 2014 at 1:27
  • This works: <label for="district" class="medium">District</label> <select name="district" class="textbox short_field"> <option value=""></option> <span id="districts_dropdown"> <option>hello</option> </span> </select> Commented Jan 10, 2014 at 1:30
  • The PHP issue aside, you can't do that with a select tag. Read the spec w3.org/TR/html401/interact/forms.html#h-17.6 "This works nicely if <span id="districts_dropdown"> is outside of the <select> tags" - the solution is obvious. Commented Jan 10, 2014 at 1:33
  • I knew it couldn't be that easy. Commented Jan 10, 2014 at 2:06

1 Answer 1

1

It looks like you want to repopulate the options in a select tag. First of all you cannot put a span in there. Please adhere to the specs - <select> tags can only contain <option> and <optgroup> tags.

This is what you're changing

document.getElementById("districts_dropdown").innerHTML=xmlhttp.responseText;

So what will happen if you target this instead

<select name="banner" id="banner" class="textbox short_field" onchange="show_districts(this.value)">

with

document.getElementById("banner").innerHTML=xmlhttp.responseText;

You get options. http://jsfiddle.net/Da26e/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. So far it has taken me one step further. I just updated my original post with a new problem. Can you take a quick look?
echo '<select name="district" class="textbox short_field"> now doesn't have an id. It needs one echo '<select id="districts_dropdown" name="district" class="textbox short_field">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.