Forum Moderators: coopster

Message Too Old, No Replies

Populating Drop Down Based On Previous Selection

Select Box

         

Go3Team

4:16 am on Sep 6, 2009 (gmt 0)

10+ Year Member



I don't know if this is possible with PHP or not, but what I am trying to do is populate a drop down box based what was selected in a previous drop down.

The first box is choosing a customer from the first drop down box, the second should reflect what computer he has brought in for service.

I've seen examples done in Javascript and AJAX, but I have no idea how to do get it to work with my variables.


<?
$sql1="SELECT * FROM customers";
$result1=mysql_query($sql1);

$options1="";

while ($row1=mysql_fetch_array($result1)) {

$id1=$row1["id"];
$thing1=$row1["name"];
$poot=$row1["phone1"];
$thing2="$thing1 - $poot";
$options1.="<OPTION VALUE=\"$id1\">".$thing2;
}
?>

Sold To:</td><td><SELECT NAME=customer><OPTION VALUE=0>Choose <?=$options1?> </SELECT>


<?
$sql4="SELECT * FROM computers";
$result4=mysql_query($sql4);

$options4="";

while ($row4=mysql_fetch_array($result4)) {

$id4=$row4["id"];
$c=$row4["customer"];
$sql6="SELECT * FROM customers WHERE id = '$c' LIMIT 1";
$result6=mysql_query($sql6);
$row6 = mysql_fetch_assoc($result6);
$name6 = $row6["name"];
$thing4=$row4["customer"];
$poot4=$row4["make"];
$stuff=$row4["serial"];
$thing5="$name6 - $poot4 - $stuff";
$options4.="<OPTION VALUE=\"$id4\">".$thing5;
}
?>

Computer:</td><td><SELECT NAME=computerid> <OPTION VALUE=0>Choose <?=$options4?> </SELECT>

Thanks.

rocknbil

4:10 pm on Sep 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean like this?


<?
$sql1="SELECT * FROM customers";
$result1=mysql_query($sql1);
$options1="";
while ($row1=mysql_fetch_array($result1)) {
$id1=$row1["id"];
$thing1=$row1["name"];
$poot=$row1["phone1"];
$thing2="$thing1 - $poot";
$options1 .= '<OPTION VALUE="' . $id1 . '"';
if (isset[$_POST['customer']) and ($_POST['customer'] == $id1)) {
$options1 .= ' selected';
}
$options1 .= '>' .$thing2 . '</option>';

}
?>

BTW always quote your attributes, even if you have to single-quote, and it's a Good Idea to ID them, especially if you apply JS at any point.

<SELECT NAME="customer" id="customer">

Go3Team

7:31 pm on Sep 6, 2009 (gmt 0)

10+ Year Member



I'm not sure how to work that in. There shouldn't be any POST data for the [$_POST['customer'] to work. I'm looking at doing it another way, I'm guessing some sort of javascript is needed, but I know nothing about javascript. Thanks.

rocknbil

8:31 pm on Sep 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure how to work that in.

Just try it, replace the bolded chunk of code with the same section in your code. I'm pretty quick with JS, and you don't need another layer of technology - this will work.

When it first loads, there will BE no $_POST['customer'], right?

if (isset[$_POST['customer'])....

It is not set so it will skip the "if" output, so you get a select list with nothing selected.

Then when they select the customer value and submit, it will be in $_POST (or $_GET, if that's your form action.)

if (isset[$_POST['customer']) and ($_POST['customer'] == $id1)) {
// it IS set, but must match on $id1, or will not add ' selected'

So as you're re-querying the database, if the id matches, it adds ' selected' to that option item.

No JS would be required for this particular problem - however, if the select list changes from the submitted value, you'd want it to re-submit to get the new values for the new selected item. So in addition to the above, you could use Javascript to re-submit the form:

<select name="customers" id="customers" onChange="this.form.submit();">

But this is in addition to your original problem, if I understand it correctly.

Go3Team

9:45 pm on Sep 6, 2009 (gmt 0)

10+ Year Member



Gotcha, I had to figure how to work it in, was concentrating too much on the $_POST thing.