tags:

views:

53

answers:

4

I will draw the options for the second select-tag from a database.

Thanks.

A: 

you have to use ajax to perform this without page submit.

Or just submit the page make the action self (the same php).

check if there is $_POST[select1value]

then populate from db in second select

<select2>
<?php
if( ($_POST['submit'])  && ($_POST['select1']) )
{
?>

<option1> of select2</option>

<?
}
?>

Thats it.

But AJAX is nice if you wan to use

zod
@zod, thanks. I submit the form at the onchange-event of the first select with the action pointing to the same file and with similar PHP code following.
Geoffrey Van Wyk
Good. Anyway you try some ajax when you get time. This dropdown population is the best example for ajax. Try. http://www.w3schools.com/ajax/ajax_database.asp
zod
A: 

Typically you have one table for each select box. Here's an example:

table1:
   id   |  item  | extra_info
   1    |    1   |    ...
   2    |    2   |    ...
  ...       ...       ...

table2:
   id   | prev_choice | item
   1    |     1       |  1_a
   2    |     1       |  1_b
   3    |     2       |  2_a
   4    |     2       |  2_b
  ...        ...         ...

This way you just search for any item in table2 with a "prev_choice" based on whatever was selected before. For instance, if table one contained a list of continents, table two could contain a list of countries where "prev_choice" refers to which continent that country is within.

The SQL query would look something like:

SELECT * FROM table2 WHERE prev_choice LIKE {$_GET["selected"]}
steven_desu
+1  A: 

Non-javascript solution:

Have the user submit the form that the select is in, so the values would be in GET/POST.

When the page appears again, PHP can access the variables to assemble the second select tag.

Something like this:

<form action='' method='post'>
<select name='select1'>
  <option value='blah'>blah</option>
</select>

<?PHP if(isset($_POST['select1']) { 
/* call DB and get infos */ 
echo "<select>";

/* FOR loop echoing <option>info</option> */

echo "</select>"; ?>
</form>

Haven't tested it but should be OK ish.

Steve
+1  A: 

There is no need to use Ajax at all if the tables are small. You can just build every possible second select box and hide it on the page and display the appropriate one based on the user's selection. This is even faster than Ajax and requires no page reloads or http requests.

If the tables are too large, though, or the second select box can have user-dependent data, then that is obviously not an option.

tandu