1

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; ?>

2 Answers2

0

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!

Community
  • 1
  • 1
jewettg
  • 1,056
  • 11
  • 20
  • Is populating the same thing? I want to make sure the corresponding value to the user access level is selected as I am returning the entire users table, not populate the drop down with those values - there are only two values. – Barney Gorilla May 24 '15 at 15:27
  • Yes, populating is the same thing! Basically you are using PHP to query the database for values (both what the user sees in the pull-down menu and the value that is returned when they submit the form). Remember, you might have a ton of code in the actual HTML file that you have on the server, but the actual HTML page served to the browser from the server is considerably smaller as the PHP code is left out (after it is executed). – jewettg May 25 '15 at 13:53
0

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>';

?>
Tamaki
  • 53
  • 7
  • This would work although increases code length. Jquery answer would be nice as I thought I was close to what I need to get it to work. – Barney Gorilla May 24 '15 at 16:10