0

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>
3
  • What does the generated HTML look like? You should also remove the space between your attributes and values in <select>, and not use the mysql_* extensions as they have been deprecated - go with mysqli or PDO instead. Commented Nov 9, 2012 at 3:26
  • It's done through an ajax request, so I'm not sure how to check the html. However, when I echo the event_id (the output), it's the last item of the list. Commented Nov 9, 2012 at 3:30
  • I'm not totally sure what you are doing, but to select a value you put "selected" on the option, not a value on the select box, and you should be passing something like JSON if it's and AJAX request, not HTML. Commented Nov 9, 2012 at 3:32

1 Answer 1

1

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)">
Sign up to request clarification or add additional context in comments.

5 Comments

value isn't even a valid attribute for <select>
I know, but I'm not about to get into random browser quirks
Thanks, this worked perfectly but I'm a bit confused. Where is javascript acquiring the value for this.value? Does it just inherit the value from the name of the div (saved_form)?
"this" refers to the current tag, and "value" is an attribute assigned to the tag, and the select tag gets its value from the value of whichever option is currently selected.
Ok, so basically it just relates it to the option tag, right? I don't use javascript much so I really apprecaite the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.