Fill In Multiple Selection Boxes From One Selection Box Sele

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Fill In Multiple Selection Boxes From One Selection Box Sele

Post by icesolid »

I have a selection box at the top of my web site and then I have a bunch of rows printed out into a table from a MySQL database below that selection box.

Each one of these rows has a selection box in the first column.

I want the selection box that is at the top of all of these rows of data to fill the same value in as it if I select a value from it.

So if I select "Apples" from the selection box at the top of the table of data rows then all of the selection boxes in the table below would fill in with Apples.

Here is the code that the selection box at the top of the table of data rows has:

Code: Select all

<select name="assign_all" class="mediumInput"> 
<option value="">Select An Inspector</option> 
<?php 
$result1 = mysql_query("SELECT * FROM users WHERE account_type='Inspector' ORDER BY username ASC"); 

while($row1 = mysql_fetch_array($result1)) { 
?> 
  <option value="<?php echo $row1["user_code"]; ?>"><?php echo $row1["username"]; ?></option> 
<?php 
} 
?> 
</select>
Here is the code that each one of the selection boxes in the table has:

Code: Select all

<select name="inspector[<?php echo $row["id"]; ?>]" class="smallInput"> 
<option value="">Select An Inspector</option> 
<?php 
$result1 = mysql_query("SELECT * FROM users WHERE account_type='Inspector' ORDER BY username ASC"); 

while($row1 = mysql_fetch_array($result1)) { 
?> 
  <option value="<?php echo $row1["user_code"]; ?>"><?php echo $row1["username"]; ?></option> 
<?php 
} 
?> 
</select>
If an inspector is select in that top box I want all of the selection boxes below to fill in with that inspector.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Good luck with that - hope you figure it out :?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

lol, thanks
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

I have no clue how else to explain it.

I want to have a selection box that is a master selection box (select all) that when a value is selected in it, it will fill in all of the selection boxes on the page with the same value it has in it.

Does that help?

feyd, where are you!?!?! lol
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Are there a set, finite number of "slave" selection boxes?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

This function looks at EVERY drop down box on the page, and if it's name attribute starts with the string "inspector", it will set the value to be whatever value you pass to the function (the actual value of the option, NOT the text that is displayed for that option).

Code: Select all

function SetValues(val) {
    var boxes = document.getElementsByTagName("select");
    for(var i=0;i<boxes.length;i++) {
        if(boxes[i].name.substr(0,9) == "inspector")
            for(var j=0;j<boxes[i].options.length;j++) {
                if(boxes[i].options[j].value == val) {
                    boxes[i].selectedIndex = j;
                    break;
                }
            }
        }
    }
}
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

Thanks for the help moose!
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

I have no errors, but when I select the main box the other boxes do not fill in?

Do I need to put some type of code in the main selection box to call the function?
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Yes, you need to set the onchange handler to fire that function, and pass it the value you want to select in the other boxes.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

I have tried this code here:

Code: Select all

onchange="SetValues();"
in my main selection box <select></select> tag.

No errors but the fields still do not fill in?

I have that javascript code you gave me underneith all of my form fields but inside of the </form> tag.
Post Reply