从MySQL在PHP中创建下拉菜单? [英] Creating a Drop Down Menu in PHP from MySQL?

查看:79
本文介绍了从MySQL在PHP中创建下拉菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP和MySQL有一定的经验,所以我掌握了一些东西,但是我想尝试获得一些可能超出我的水平的东西(不确定这个的困难程度).基本上,我希望创建2个下拉菜单,以删除MySQL表中用户的访问权限.因此,第一个下拉菜单将用于我拥有的用户列表,第二个下拉菜单将用于他们具有的访问权限.我希望选择一个用户时,第二个下拉菜单仅显示该用户有权访问的选项;

I'm sort of experienced with PHP and MySQL so I kind of get the hang of things, however I'm sort of trying to get something that may be over my level (not really sure the difficulty level on this). Basically, I am looking to create 2 drop down menus to remove access from a user within the MySQL table. So the first drop down menu will be for the list of users I have, and the the second drop down menu will be for the access they have. I'd like for it to when I select a user, the second drop down menu only shows the options that the user has access to such as;

    <form name="form" method="post" action="access.php">
    <select name='user'>
    <?php 
require "sqlconfig.php"; // My SQL Configuration
$conn=mysql_connect($host, $user, $pass); // The Connection
$selectdb=mysql_select_db("$db"); // The Database
    $userlist1 = mysql_query("SELECT `Username` FROM `accounts` ORDER BY Username ASC");
        while ($userlist = mysql_fetch_array($userlist1)) {
        print "<option value='username'>$userlist[Username]</option>";
        }
    ?>
    </select>
<select name='task'>
<option value="notask" <?php if($_GET['task'] == notask) { echo "selected"; } ?>>Select a Task</option>
<option value="admin" <?php if($_GET['task'] == admin) { echo "selected"; } ?>>Administrator</option>
<option value="user" <?php if($_GET['task'] == user) { echo "selected"; } ?>>user</option>
<option value="guest" <?php if($_GET['task'] == guest) { echo "selected"; } ?>>Guest</option>
</select>
<input type="submit" value="Remove" />
    </form>

我已经在一个表中具有权限,每个值都为整数0(表示false)和1(表示true).因此,基本上,当从第一个菜单中选择用户时,我希望刷新第二个菜单,并且只显示其具有的值= = 1(这意味着他们具有该值),然后显示提交表单时的值. ,它将选择的任务从1更改为0,这意味着它会自行删除.

I already have the permissions in a table, each value is as integer 0 for false, and 1 for true. So basically when the user is selected from the first menu, I want the second menu to refresh, and only show the values they have that are = to 1 (which means they have it), and then of course, when the form is submitted, it will change whichever task was selected from 1 to 0, meaning it removes itself.

基本上,当用户选择(例如;乔)(;联系,用户EX),所述第二菜单将与Joe有任务刷新.一旦我选择了一个任务然后单击删除",该脚本应发布用户名菜单,并获取提交前选择的所有任务,然后运行该脚本以将"Admin"之类的用户字段从1(true)更新为0(false) )

Basically, when the user is selected (ex; Joe), the second menu will refresh with the tasks that Joe has (ex; Admin, User). Once I select a task then click remove, the script should post the username menu, and get whatever task that was selected before submit, then run the script to UPDATE the users field of like 'Admin' from 1(true) to 0(false)

有人可以帮我用代码进行设置,还是至少给我指导以自己进行设置?

Can anyone help me set it up with code, or at least give me the direction to set it up myself?

推荐答案

创建一个div并为其指定一个id="result",然后在该div中放置一个空的select标签.

Create a div and give it a id="result" and put an empty select tag in that div.

然后使用jQuery + Ajax,使用以下命令获得第二个下拉列表:

Then using jQuery + Ajax, get the second dropdown using:

$('select[name="user"]').change(function(){
   var user = $(this).val();

   $.ajax({
      type:'post',
      url:'getSecondDropDown.php',
      data:'user='+user,
      success:function(result){
         $('div#result').html(result);
      }
   });

});

在您的"getSecondDropDown.php"文件中,包括以下内容:

In your 'getSecondDropDown.php' file, include this:

<select name="task">
<?php
$user = $_POST['user'];
$q = mysql_query("SELECT task FROM tablename WHERE user=".$user.") or die();
while($r = mysql_fetch_array($q))
{ ?>
  <option value="<?php echo $r['task]; ?>"><?php echo $r['task]; ?></option>

<?php
}
?>
</select>

这篇关于从MySQL在PHP中创建下拉菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆