Hi

I have what is probably a simple question as still fairly new to php.

I have a webpage which has two drop down lists, both dynamically created from sql databases.

I need the user to be able to select a client in the first drop down list and it to then populate the second list with contacts for that client.

At present I can get the form to post back to itself but the first list keeps reverting to the last option in the list.

Here is the code for to create the 1st drop down as well as the code to capture the selected option.

<form name="choose client" action="new_job_form.php" method="post">
  <select name="select_client" onchange="this.form.submit()" ;>

  <?php

       $query1 = "SELECT * FROM clients ORDER BY clientName";
        $result = mysql_query($query1);
            if (!$result) { 
                die(mysql_error());
        }

        while ($row = mysql_fetch_assoc($result)) {


echo '<option value = "' . $row['ClientId'] . '" selected="'.$row['ClientName'].'" name="' . $row['ClientName']. '" >' . $row['ClientName']. '</option>';

        } 


?>

</select>
</form>

This populates the list as I need it to but isn't catching the data correctly.This is the code for the 2nd drop down.

<select name="contact">
<?php      




$query2= "SELECT * FROM client_contact, clients WHERE client_contact.ClientID=clients.clientID";
$result2 = mysql_query($query2);
        if (!$result2) {
            die (mysql_error());
            }
  ?>    
  </select>

Apologies if the code is not the tidiest in the world - I am still learning :)

Many thanks in advance for your help.

The key to solving this problem is how the form is submitted when the user selects from the first drop down. When the form is submitted, their selection is placed in the superglobal array $_POST with the array key equal to the name parameter of your select tag. Your form action tells the page to go to the script new_job_form.php when the form is submitted which is hopefully the same script. Now what you need to do is retrieve the results of the form's submit like so:

if(isset($_POST['select_client']))
   $clientId = intval($_POST['select_client']);
else
   $clientId = -1; // or some other value which indicates none selected
// you should validate $clientId and make sure that it is not invalid here

Ok, now that we have our client id, how to use it? First we need to set the fact that the client is selected in the first select, so change your first select tag to look like this:

<select name="select_client" onchange="this.form.submit()" value="<?php echo $clientId; ?>" >

Now in your SQL statement for selecting the options for the second select tag, change it to the following:

$query2= "SELECT * FROM client_contact, clients WHERE client_contact.ClientID=clients.clientID AND clients.clientID = $clientId";

Hope this helps, although I must admit I haven't tested the code I have given you so you may need to fiddle a little bit to get it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.