Forum Moderators: open

Message Too Old, No Replies

Ajax Drop down

Ajax drop down

         

Asbillszit

6:10 am on Jul 18, 2008 (gmt 0)

10+ Year Member



I have one drop box that populates from a mysql database via php. I want another drop down to populate based on what is selected from that first drop box.

I have never used ajax before but I know php/mysql well.

Here is my code that populates the first box:

<SELECT name="owcdate">
<option value='' SELECTED> -

<?

include 'connection.php';

$gtdp2 = date('m/d/y', strtotime('+2 Day'));

$query="SELECT * FROM avail WHERE owcdate='$gtdp2' LIMIT 1";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$owcdate=mysql_result($result,$i,"owcdate");

echo "<option value='$owcdate'>$owcdate";

++$i;
}

mysql_close($link);

?>

</select>

[edited by: Asbillszit at 6:11 am (utc) on July 18, 2008]

Fotiman

4:02 pm on Jul 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't thought out the specifics, but you'll need to add an event listener to your select element (onchange?). That listener will need to open an AJAX connection to the server and process the reply from the server. So you'll need a PHP page on your server that accepts a query and returns a partial value (ie - not a whole web page). It could return a string containing all of the option elements if you wanted to do something similar to the code you have above, or you could serialize an array of results and parse that with JavaScript, using JavaScript to create the option elements.

If you want to simplify the AJAX part, you could use something like the Yahoo UI Library [developer.yahoo.com]'s Connection Manager. It handles the cross-browser issues providing you with an easy to use and well documented API.

Asbillszit

11:41 pm on Jul 18, 2008 (gmt 0)

10+ Year Member



Thank you. I got the issue fixed with learning and writing an AJAX script.