user1406951
user1406951

Reputation: 446

PHP dropdown only selecting the last item in list

I created a drop down list in PHP and it's selecting the last item of the list and I can't figure out why it's doing this.

When I echo the event_id (the value that is returned), it's the last item every time. The list is populated correctly.

$forms = mysql_query("select events.event_title, events.event_id, saved_forms.id from events
                     INNER JOIN saved_forms on saved_forms.id = events.event_id
                    where saved_forms.form_type = 'e' and events.event_by = '$my_username'");   

while($form = mysql_fetch_array($forms)){
    $form_id = $form['event_id'];
    $selection.="<OPTION VALUE=\"$form_id\">".$form['event_title']."</OPTION>";
}

?>
<div id="saved_forms">
<tr><td><select name ="saved_form" value ="<? echo $form_id; ?>" onchange="showUser(<? echo $form_id; ?>)">
<Option value="$form_id"><? echo $selection; ?></Select></td><td>Select Existing Form</td></tr>
</div>

Upvotes: 0

Views: 806

Answers (1)

quentinxs
quentinxs

Reputation: 866

Your problem is here

<select name="saved_form" value="<? echo $form_id; ?>" onchange="showUser(<? echo $form_id; ?>)">

By giving the select tag a value of $form_id you have a high chance of overriding the value set in the options. And you shouldn't hardcode the value into the onchange event. Try removing the value from select and let the option tags do the work.

<select name="saved_form" onchange="showUser(this.value)">

Upvotes: 1

Related Questions