简体   繁体   English

在PHP中填充下拉框

[英]Populating drop down boxes in php

just a quick one im looking to populate three drop down boxes to filter data and each one of them is going to affect the next. 很快,我就希望填充三个下拉框来过滤数据,而每个下拉框都会影响下一个。

what i want is for 我想要的是

Drop down 1 has company 下拉1有公司

once drop down 1 is selected the second drop down is populated with the branches for that company 一旦选择下拉菜单1,第二个下拉菜单将填充该公司的分支机构

once selected the third drop down is populated by the staff members for that company in that branch then when i press search it should pull the data for that 1 staff member. 一旦选择了第三个下拉列表,则由该分支机构中该公司的工作人员填充,然后当我按“搜索”时,它应该提取该1个工作人员的数据。 all of the information is in one table 所有信息都放在一个表中

the table i have is called "stafflist" 我的桌子叫做“工作人员”

the columns are "company", "branch" and "staffname" each staff member has an autonumber id field which i use as a lookup called "staffID" 列是“公司”,“分支机构”和“工作人员名称”,每个工作人员都有一个自动编号ID字段,我将其用作称为“ staffID”的查找

Thank you for any help 感谢您的任何帮助

Regards Slowie 关于斯洛维

Lets take an easy example, This is a javascript solution. 让我们举一个简单的例子,这是一个javascript解决方案。 I'm using this and it works perfectly fine. 我正在使用它,并且效果很好。 This script work in case if you select a country it populates its corresponding cities in the second dropdown. 如果您选择的国家/地区在第二个下拉列表中填充了相应的城市,则此脚本有效。 You can take some idea and use this for your case where you can deal with three dropdowns respectively. 您可以采取一些想法,并将其用于需要分别处理三个下拉菜单的情况。

This is the country dropdown: 这是国家/地区下拉列表:

<?php
        $countrylist=mysql_query("SELECT * FROM country ORDER BY name ASC");
        echo "<select name='country' id='country' onchange=\"reload(this.form)\" title='Country e:g; United Kingdom,Pakistan'><option value='0'>Select Country</option>";
        while($clist=mysql_fetch_array($countrylist))
        {
        echo "<option value='$clist[Name]'>$clist[Name]</option>"."<br/>";
        }
        echo "</select>";
 ?>

This is the region dropdown: 这是地区下拉列表:

<select name="region" id="region" ></select>

Now make a seperate file named crlist.js and include it in the page having above code like this: 现在,创建一个名为crlist.js的单独文件,并将其包含在具有上述代码的页面中,如下所示:

<script  type="text/javascript" src="crlist.js"> </script>

code for crlist.js: crlist.js的代码:

var request = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
request = false;
}
}
@end @*/
function fillSelect(country,path) {
var url = path+"crlist.php?country=" + country;
request.open("GET", url, true);
request.onreadystatechange = go;
request.send(null);
}

function go() {
if (request.readyState == 4) {
//if (request.status == 200) {

var response = request.responseText;

var list=document.getElementById("region");
            for (i = list.length - 1; i>=0; i--) {
                list.remove(i);
            }
var records=response.split('|');
for (i=1; i<records.length; i++) {
    //alert("rcord="+records[i]);
    var record=records[i].split('*');
    var region=record[0];
    //alert("region="+region);
    var regionid=record[1];
    //alert("regionid="+regionid);
    var x=document.createElement('option');
    //var y=document.createTextNode(region);
    x.text=region;
    //x.value=region;
    //alert(x.text);
   //x.appendChild(y);
   //list.appendChild(x);
   list.options.add(x);
   }
  //}
 }
}

function initCs(path) {

if (!request && typeof XMLHttpRequest != 'undefined') {
request = new XMLHttpRequest();
}
var country=document.getElementById('country');
    country.onchange=function() {

        if(this.value!="Select") {

            var list=document.getElementById("region");
            for (i = list.length - 1; i>=0; i--) {
                list.remove(i);
            }
        //while (list.childNodes[0]) {
        //list.removeChild(list.childNodes[0]);
        //}
        }
        fillSelect(this.value,path);
        //alert(this.value);

    }
//fillSelect(country.value);
}

Now make a seperate file named crlist.php. 现在,创建一个名为crlist.php的单独文件。

Code for crlist.php: crlist.php的代码:

<?php
require_once 'yourconfigfile.php';

$cname = $_GET['country'];

$query="select ID,Name from city where CountryCode=(select code from country where name='$cname') Order By Name ASC";
$res = mysql_query($query) or die(mysql_error());
while($region = mysql_fetch_array($res)){
    echo "<option value='".$region['Name']."'>".$region['Name']."</option>";
}       
?>

Now add following script on the page having dropdowns: 现在,在具有下拉菜单的页面上添加以下脚本:

<script  type="text/javascript" src="crlist.js"> </script>
<script>
$(document).ready(function() {

    initCs("");

});
</script>

This is my own script, and i've assumed that you have created country and region tables. 这是我自己的脚本,我假设您已经创建了国家和地区表。 But you need to tweak the queries and above code according to your db structure. 但是您需要根据您的数据库结构调整查询和上面的代码。 In your case you have to create tables for company, branches and employees. 对于您的情况,您必须为公司,分支机构和员工创建表。

Hope this helps. 希望这可以帮助。

Have you ever heard about AJAX() OR jQuery ? 您是否听说过AJAX()jQuery if not then refer those link first; 如果没有,请先参考那些链接; actually for your task you need to use Ajax OR jquery based dropdown boxes. 实际上,对于您的任务,您需要使用基于Ajax或jquery的下拉框。 which can make your dream success. 这可以使您的梦想成功。 :) :)

refer below link for your solution . 请参考下面的链接为您的解决方案

If any help needed you can feel free to ask me.. 如果需要任何帮助,您可以随时问我。

EDIT As per your commnet : 编辑根据您的commnet:

Step-1:- "SELECT company_id,company_name from company_table WHERE $condition ";

Step-2:- Fill first drop down box with record of first query set.

Step-3:- call jquery.ajax function onchange event of first drop down box in which call one 
php page i.e: getRecords.php.

Step-4:- In getRecords.php page you need to get all the Branches of that company by passing company id in ajax and return a record array as response.

Step-5:- Fill second drop down with these records and again onchange event call another jquery.ajax request for final drop down box and do all things same as Step-4.

I think its all steps what you need for. 我认为这是您需要的所有步骤。 still have any issue let me inform. 还有任何问题让我通知。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM