views:

606

answers:

1

I'm populating select box options via jquery and json but I'm not sure how to address multiple instances of the same chained select boxes within my form.

Because the select boxes are only rendered when needed some records will have ten sets of chained select boxes and others will only need one. How does one generate unique selects to support the auto population of secondary select options?

Here's the code I'm using, and I thank you in advance for any insight you may provide.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function populateACD() {
        $.getJSON('/acd/acd2json.php', {acdSelect:$('#acdSelect').val()},
function(data) {
                var select = $('#acd2');
                var options = select.attr('options');
                $('option', select).remove();
                $.each(data, function(index, array) {
                  options[options.length] = new Option(array['ACD2']);
                });
        });
}
$(document).ready(function() {
        populateACD();
        $('#acdSelect').change(function() {
                populateACD();
        });
});
</script>


<?php
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$squery = "SELECT ACD1ID, ACD1 from ACD1";
$sdata = mysqli_query($dbc, $squery);
  // Loop through the array of ACD1s, placing them into an option of a select
echo '<select name="acdSelect" id="acdSelect">';
while ($row = mysqli_fetch_array($sdata)) {
echo "<option value=" . $row['ACD1ID'] . ">" . $row['ACD1'] . "</option>\n";
    }
  echo '</select><br /><br />';

<select name="acd2" id="acd2">
</select>

acd2json.php

<?php
$dsn = "mysql:host=localhost;dbname=wfn";
$user = "acd";
$pass = "***************";
$pdo = new PDO($dsn, $user, $pass);
$rows = array();
        if(isset($_GET['acdSelect'])) {
          $stmt = $pdo->prepare("SELECT ACD2 FROM ACD2 WHERE ACD1ID = ? ORDER BY ACD2");
          $stmt->execute(array($_GET['acdSelect']));
          $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($rows);
?>
A: 

If you give each "select" (well, each one that you need to control) a "class" name that identifies them as such, then you can set up a handler for changes to all of them like this:

$('select.controlMe').click(function() {
  // ... whatever
});

Your select elements would look like this:

<select name='paramName' class='controlMe'>

Now, if the <select> elements need to have specific relationships to other <select> elements, then you'll have to have a way of describing that. You could use the "metadata" jQuery plugin so that the select elements themselves could be annotated (again, in the "class" value) and the Javascript event handler could just look at that. I'm not exactly sure what it is that your code does, however.

The metadata plugin is here: http://plugins.jquery.com/project/metadata

Pointy
Thanks again Pointy, I'm a javascript rookie so your answer went over my head.I would like to explain better what I'm trying to do with this code. A table listing of sub-items related to a main record.If two of the sub-items are undefined (NULL) then I need the user to make two select choices for those values. The second select values however, are dependent on the first one, so it cascades or is chained, I've seen it labeled several different ways.When you say "if the <select> elements need to have specific relationships to other <select> elements" is that the same as being dependent?
Aaron
I know that's not what you wanted to read though...I'm just going to go back to google and see if I can find an example out there somewhere.
Aaron
Yes, by "relationship" I think I meant the same sort of thing as you mean by "dependent". The point is, one `<select>` makes things happen to another one, and somehow the code needs to know which `<select>` drives which others. There are lots of different ways to achieve that - you could use a simple naming convention, for example.
Pointy
You make it sound simple but it seems quite complex to me. I get your point regarding the relation and I have it working on one set of selects, but saying "you could use a simple naming convention, for example." still doesn't help me understand how to differentiate within javascript for the x amount of related selects that could be rendered. I suppose I'll go hire an expert for a couple of hours to break it down in a way I can relate/understand, nothing in this world is free and I should've known. I do thank you for your time today Pointy.
Aaron