Forum Moderators: coopster

Message Too Old, No Replies

populated drop-down box

populated drop-down box from mysql

         

snowman304

8:28 am on Aug 13, 2007 (gmt 0)

10+ Year Member



I am trying to populate a drop-down box in a form. Not only that, but if the user has a error with there form submission, I want there selection be retained when they are sent back to fix there error.

The $baitlist[baitname] are being echo as $baitlist[baitname] and not the value. Which leads me to believe that my problem is with not using ' and " correctly. Can someone help me sort this out?

<?php $baitsql="SELECT baitname FROM baits WHERE memberid = '$_SESSION[MEMBERID]' ";
$baitresult = mysql_query ($baitsql);
echo '<select name="bait_name" id="bait_name">';
echo '<option'.($_SESSION['bait_name']==""? ' selected' : '').' value=""></option>';

while($baitlist=mysql_fetch_array($baitresult)) {
echo '<option'.($_SESSION['bait_name']=="$baitlist[baitname]"? ' selected' : '').' value="$baitlist[baitname]">$baitlist[baitname]</option>';
}
echo '</select>';?>

Thanks,
Mike

dreamcatcher

8:34 am on Aug 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried removing the double quotes?

dc

Habtom

8:37 am on Aug 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the following:

echo "<option".($_SESSION['bait_name']=="$baitlist[baitname]"? ' selected' : '')." value='". $baitlist['baitname'] ."'>". $baitlist['baitname'] ."</option>";

snowman304

9:11 am on Aug 13, 2007 (gmt 0)

10+ Year Member



Thanks Habtom, That works as planned.

Thanks again,
Mike