Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

value of drop down 2 depends on value of drop down 1 ; PHP

I have two drop down menus that are being dynamically populated from an mysql database.
Drop down 1 will look like this:
$stmt2 = $db->query('SELECT vendor_id, vendor_name from vendor_inbound ');
$vSelect2 = '<select size="1" name="vendor">' . "\n";
while($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
$vSelect2 .= '<option value="'.$row2['vendor_id'].'">'.$row2['vendor_name'].'</option>' . "\n";
}//end while
$vSelect2 .= '</select>' . "\n";

Open in new window

.

Now depending on the vendor ID my next field on my form (which is a drop down menu) needs to be populated accordingly. My table looks like this;

ph_id      vendor_id              number      status
1                 1                        616      1
2                 1                        214      1
3                 2                        845      1
4                 2                        801      1

My second menu item should look like this:
$stmt3 = $db->query('SELECT number from did where vendor_id ="$id" ');
$vSelect3 = '<select size="1" name="did">' . "\n";
while($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
$vSelect3 .= '<option value="'.$row3['number'].'">'.$row3['number'].'</option>' . "\n";
}//end while
$vSelect2 .= '</select>' . "\n";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aej1973
aej1973

ASKER

Hello leakim971, thank you very much for your solution, it works great but for one issue. Drop down 2 is a part of a form, when I hit the submit button the values from the other fields in the form get passed but not the value which I select from dropdown 2. How do I overcome this? Thank you for your help.

A
if the value is not passed, maybe
[  ] - your page/form is malformed (can check that for you, go to http://validator.w3.org/)
[  ] - the field is outside the submitted form (look like it's inside)
[  ] - there's no name attribute to the field (look like there's one)

There's no relative issue with cascading dropdown and ajax
Avatar of aej1973

ASKER

Thank you very much it works!  I appreciate your help.

A