0

I have a select filled from database:

<?php
$query = mysql_query("SELECT * FROM users");
if(mysql_error())
{
print(mysql_error());
}
echo'<select id="dropdown2" id="user_id" name="user_id" data-placeholder="[Select]" class="select" style="width:350px;" tabindex="2")">';
echo'<option value=""></option>';
{
while($row = mysql_fetch_array($query))
echo'<option value="'.$row['user_id'].'">'.$row['user_name'].'</option>';
}
echo'</select>';                                                        
?>

I need to on submit pass dropdown value on js variable

I'm trying in this way, but variable is blank:

<script language="JavaScript">
var dropdown_value = $('#user_id option:selected').val();;
</script>

Any help?

3
  • You are using an obsolete database API and should use a modern replacement. Commented Apr 22, 2013 at 5:36
  • 1
    <script language="JavaScript"> — Why HTML 3.2? Wait data-placeholder is HTML 5+ only. Your HTML is just invalid. Validators are really useful tools, you should make more use of them. Commented Apr 22, 2013 at 5:37
  • Pretty sure the issue is that you need to wrap your code in an event listener for when the value of the dropdown changes. Not enough time right now to code it out but it looks like that code will run once on load (Before the user selects one) then never again. Commented Apr 22, 2013 at 5:42

3 Answers 3

3

Your select element has two ID attributes: <select id="dropdown2" id="user_id"

This is invalid. Your browser is probably trying to recover from the error by ignoring the second one (which is the one you are trying to access).

Testing your code with a validator would have picked this problem up.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to access the value of the <select> and you need to make sure you assign only one id attribute to it because currently there are two:

var dropdown_value = $('#user_id').val();

1 Comment

@Quentin I missed the fact that there are two different id attributes on the same elements. +1 for you
-1

first of all make the first one option's value outside the while loop 0 then in jquery function var dropdown = $('#dropdown2 option:selected').val();

Comments

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.