Reputation: 446
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
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