Jump to content

dynamic select menu problem


seveneleven

Recommended Posts

Hi, I am trying to build a cascading select menu. I have it working except for this part. The very first menu should list 4 different events. When I test the page it only displays the first one. The code I am using is below.

 

<select name="event" onChange="autoSubmit();">
        <option value="null"></option>
        <option value=" echo $row_events['event_id']; ?>" <?php if($event == $row_events['event_id']) echo " selected"; ?>><?php echo $row_events['event_no']; ?></option>
       
    </select>

 

Any help is appreciated. Thank you

Link to comment
https://forums.phpfreaks.com/topic/205804-dynamic-select-menu-problem/
Share on other sites

It's doing exactly as it should do, according to the code. There are 2 <option></option> tags, one has a NULL value, the other has what appears to be data from a DB query, however, the second <option> needs a PHP open tag before the first echo() statement, and I believe the technically proper html syntax to preselect an <option> item is selected="selected", rather than just "selected".

Hi thank you for your help. The database column that I am calling has 4 entries. How do I get it to display all 4 and not just the first?

 

Here is more code I have tried where I use the query inside the php tags. Results are parse errors, and I cant find what it is.

 

select name="event" onChange="autoSubmit();">
        <option value="null"></option>
        <option value=" 
	<?PHP 
	$sql="SELECT events.event_id, events.event_no FROM events WHERE events.event_date >= current_date";
	$event = mysql_query($sql,$conn);
	echo $row_event['event_id']; if($event == $row_event['event_id']) echo {"selected"};> echo "{$row_event['event_no']};
	</option>"?>
    </select>

You would need to echo the data from within a loop. I obviously haven't tested this, but give it a try.

 

 
<option value="null"></option>
<?php
$sql="SELECT events.event_id, events.event_no FROM events WHERE events.event_date >= current_date";
$event = mysql_query($sql,$conn);
while( $array = mysql_fetch_assoc($event) ) {
     echo "<option value=\"" . $array['event_id']";
     if( $event == $array['event_id']) {
          echo "selected=\"selected\"";
     }
      echo ">" . $array['event_no'] . "</option>\n" 
}
echo "</select>\n";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.