I have been looking and experimenting for the past week for a way to to have one drop down list (populated from a MySQL DB) determine the value of a second drop down list (also populated from a MySQL DB) after the user makes a selection from the first drop down.
I want to stay in the PHP realm so it will require a refresh. I found a script posted by Toly which uses a form with the method="get" which works fine on it's own. Now my situation...
I have an input form that is used to add clients to a DB (MySQL of course). The two drop down lists I am working with are on that form. This form's method="post". I can get the first drop down to load using the onload event for the page. No problems. The problem is the second drop down. How can these two forms exist together, one within the other, or is that even possible?
If it is not possible to have one form within another (which I suspect) has anyone every had to solve this problem, and if so how did you do it? I have some ideas, but none of them seem to be working when I think them through and draft them on paper. If it is possible to have a form within a form how does that happen?
Basically, you will choose a value for the first drop down the first time you submit the form, once submitted, $_POST will be set. Check whether or not it is set and if it is set, use $_POST['dropdownmenuname'] to detect the value of the first drop down menu, and then display different drop downs for the second menu according to what the variable contains.
Now you probably have realised the problem that when the form is submitted, no processing will occur. Well, what can be done is to use sessions to set whether or not the form has been submitted once or twice. Twice means that the client can be added.
So in the code that processes the form, you have to basically make the entire form appear again, and just make the second menu dynamic. Then you would have a check to see, at the start of the processing, whether or not the form has been submitted once or twice.
OK, that makes some sense to me (did I mention that I am pretty new to PHP ).
I have added the info to the form as I think you mean. The code snippet is below
[PHP]
<select name="fp_id" id="fp_id" onChange="<?php echo $PHP_SELF; ?>">
<?php
$result_coy = getcoynames();
while ($row = mysql_fetch_array($result_coy)) {
// determine value for $fpname
if ($row['coy_name'] <> "") {
$fpname = $row['coy_name'];
} else {
$fpname = $row['name'];
}
// check if the $_POST var is set and equal
// to the value of
if (isset($_POST['fp_id']) == $row['fp_id']) { echo '<option value="'.$row['fp_id'].'" selected>'.$fpname.'</option>';
} else {
echo '<option value="'.$row['fp_id'].'">'.$fpname.'</option>';
}
}
?>
[PHP]
Populating the list box works just fine. When I select an item, I would expect the page to reload and do some stuff. The form method="post" but it is not being set. Obviously the $PHP_SELF in the onChange event does not do what I want it to do. What should I do here to get the value set in the $_POST['fp_id']variable.
Bookmarks