...

View Full Version : Drop down default option



Kal
02-10-2006, 03:51 PM
Hi

I have coded the following which creates me a database driven drop down menu, however i want to be able have a default option, which is automatically selected based on the value of a varaible.

Any help would be great.

<?php

$query = "select scriptname from script_summary";
$result = mysql_query ($query) or die (mysql_error());
echo "<td><b>Please Select A SCRIPT:</b></td> <td><SELECT name=\"scriptname\">";
if (mysql_num_rows($result)>0)

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

{
echo "<option value=\"$row[scriptname]\">$row[scriptname]</option>";
}
}
echo "</SELECT></td>";
?>

Thanks in advance.

degsy
02-10-2006, 04:03 PM
echo '<option value="' . $row['scriptname'] . '"';
if($row['scriptname'] == $myvar){
echo ' selected';
}
echo '>' . $row['scriptname'] . '</option>';

Kal
02-10-2006, 04:12 PM
Thanks for that works a treat.

i dont suppose you know how to populate a second drop down list based on the first one do you?

degsy
02-10-2006, 04:54 PM
Create A second query that depends on the value of the submitted select box value

Here is an example
http://degs.cybertechhelp.com/select.php

Kal
02-10-2006, 04:59 PM
Do i need to submit the the selection from the first drop down, and then populate the second one. Cause i wouldnt want to submit anything until the rest of my form was filled in.

degsy
02-13-2006, 05:00 PM
If you don't want to submit it then you would need to use javascript

http://degs.cybertechhelp.com/mx_select.php

You would need to output all of your queries into a javascript array that you can then use to populate the drop downs.

Kal
02-13-2006, 05:14 PM
Right okay cheers, im ok at doing it that way, did it before, just thought it would be good if i could do it all with PHP.

thanks anyway

degsy
02-14-2006, 03:33 PM
You can do similar using ajax
http://computer-helpforum.com/select_via_ajax.php

http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm#combo



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum