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?

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
user2112020
  • 97
  • 2
  • 8
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Apr 22 '13 at 05:36
  • 1
    ` – Quentin Apr 22 '13 at 05: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. – Chris O'Kelly Apr 22 '13 at 05:42

3 Answers3

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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();
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
-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();

Faryab
  • 1
  • 2