I am querying a mysql database and returning some user info including their access level. As the user level either 1 or 2 comes back from the database how do I have their user level selected using jQuery.
<select id="dropdown">
<option value="1">Admin</option>
<option value="2">General</option>
</select>
I want to set accesslevel to be what ever access level the user has:
$(document).ready(function(){
$("#dropdown").val('accesslevel');
});
Sorry but I am an absolute beginner. I tried to get access level from php variable name using this but its not working.
<?php $accesslevel; ?>
What you are trying to do has already been answered numerous times. Try searching within Stack Overflow using the keywords:
Database select populate option and select HTML
I found the following for you:
Hope this helps!
You don't even need to use jQuery for that, it can be handled with just PHP like this:
<?php
echo '<select id="dropdown">';
if ($accesslevel == 'admin') {
echo '<option value="1" selected>Admin</option>';
echo '<option value="2">General</option>';
} else {
echo '<option value="1">Admin</option>';
echo '<option value="2" selected>General</option>';
}
echo '</select>';
?>