简体   繁体   English

选择选项以基于mysql填充的下拉菜单更新第二个选择选项

[英]select option to update second select option based on mysql populated dropdowns

I am trying to create a smart form which will automatically limit the options available of a second drop down box. 我正在尝试创建一个智能表单,该表单将自动限制第二个下拉框的可用选项。

Background: I am creating a ticketing system and I would like for the user to select a site from the "Site Selection" drop down and then the "User Selection" drop down will only contain users linked to the site selected on the first drop down, all information populated from MySQL. 背景:我正在创建一个票务系统,我希望用户从“站点选择”下拉列表中选择一个站点,然后“用户选择”下拉列表将仅包含链接到在第一个下拉列表中选择的站点的用户。 ,所有信息均来自MySQL。

Future: I would then like to list all the services associated to that user with tick boxes under "Select Affected Services" 未来:我想在“选择受影响的服务”下的复选框中列出与该用户关联的所有服务

My js is pretty poor. 我的js很差。 So far I have been able to find some code by searching here that has allowed me to write the User's Computer ID into a text field. 到目前为止,我已经可以通过在此处搜索找到一些代码,从而使我可以将用户的计算机ID写入文本字段。 But I cannot figure out how to capture the output and query for the Computer Name (different table) and display that as a tick box option or to use the same method to limit the next lot of selection boxes. 但是我无法弄清楚如何捕获输出并查询“计算机名”(不同的表)并将其显示为复选框,或者使用相同的方法来限制下一批选择框。 All code listed below I have not cleansed yet, alpha phase. 下面列出的所有代码我都还没有清洗,阿尔法阶段。

I realise that I could do this in steps using PHP only, but I am trying to pretty up my coding here and there. 我意识到我只能使用PHP分步进行此操作,但是我试图在这里和那里弄清楚我的编码。

Displaying Drop Downs: 显示下拉列表:

    <h3>Assign Site</h3> 

    <select id="site_add" name="site_add" style="width:217px; height:20px !important;">
        <option value="-1"></option>';

  $o = "SELECT * FROM company_sites WHERE company_id = " . $company_id . " ORDER BY sitename";
$rs = mysql_query($o);
$nr = mysql_num_rows($rs);
for ($i=0; $i<$nr; $i++) {
$r = mysql_fetch_array($rs);
        echo '<option value="' . $r['id'] . '">' . $r['sitename'] . '</option>';
    }

    <h3>Assign User</h3> 
    <select id="comp_add" name="comp_add_1" style="width:217px; height:20px !important;">
        <option value=""></option>';

  $o = "SELECT * FROM company_staff WHERE company_id = " . $company_id . " ORDER BY id DESC";
$rs = mysql_query($o);
$nr = mysql_num_rows($rs);
for ($i=0; $i<$nr; $i++) {
$r = mysql_fetch_array($rs);

    $user_computer_id = $r['computer_id'];

        echo '<option value="' . $r['id'] . '">' . $r['firstname'] . ' ' . $r['lastname'] . '</option>';
    }

Current JS to output selected user's computer ID into text field: 当前的JS,用于将所选用户的计算机ID输出到文本字段中:

<script type="text/javascript"> 
window.onload=function() { attachBehaviors(); }; 
// 
function attachBehaviors() { 
 document.getElementById(\'person\').onchange=function() { 
 loadUser(this.options[this.selectedIndex].value); // <-- check this, may be incorrect 
 } 
} 
function loadUser(optionvalue) { 
 // Always set a default 
 if (optionvalue==\'\') {  
 return; 
 }
 opts = optionvalue.split(\':\'); 
 var name = opts[0]; 
 var email = opts[1]; 
 document.getElementById(\'computer_id\').value=name; 
} 

</script> 

<select name="person" id="person"> 
    <option value=""></option>';


$result=mysql_query("SELECT * FROM company_staff"); 
while($row=mysql_fetch_array($result)) { 

$submit_firstname = $row['firstname']; 
$submit_lastname = $row['lastname']; 
$submit_staff_id = $row['id']; 
$submit_computer_id = $row['computer_id']; 


 echo '<option value="' . $submit_staff_id . '">' . $submit_firstname . ' ' . $submit_lastname . '</option>\n'; 
} 
echo '
</select> 
<input type="text" id="computer_id" name="name" placeholder="name" /> 
';

Any recommendations on how to achieve this would be greatly appreciated! 任何有关如何实现这一目标的建议将不胜感激!

Thanks in advance! 提前致谢!

You need AJAX, it's a combination of PHP and JS. 您需要AJAX,它是PHP和JS的组合。 ( javascript request data from the server, php processes the request, returns data ). (来自服务器的javascript请求数据,php处理请求,返回数据)。

You would need to practice AJAX a bit to understand how to make this select box. 您需要稍微练习一下AJAX,以了解如何制作此选择框。

This is process in theory: 理论上这是过程:

  1. You output your first select box with PHP. 您使用PHP输出第一个选择框。
  2. Make an onchange event handler for it that would call a server for information. 为它创建一个onchange事件处理程序,该处理程序将调用服务器以获取信息。

    ..options.. ..options ..

  3. This updateNewDropDown function will take the value form a server response you selected and fetch the new data via PHP for the second select menu, and create the new select menu with the data AJAX response provided. 此updateNewDropDown函数将从您选择的服务器响应中获取该值,并通过PHP获取第二个选择菜单的新数据,并使用提供的数据AJAX响应创建新的选择菜单。

AJAX TUTORIALS: AJAX教程:

http://api.jquery.com/jQuery.ajax/ http://www.w3schools.com/ajax/default.asp http://api.jquery.com/jQuery.ajax/ http://www.w3schools.com/ajax/default.asp

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

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