简体   繁体   English

从下拉菜单中自动填充两个输入框

[英]Auto-populate two input boxes from a dropdown menu

I am trying to fetch two variables from a mysql db table which is supposed to auto-populate once a user selects a dropdown option. 我试图从一个mysql db表中获取两个变量,一旦用户选择了一个下拉选项,该表应该自动填充。

The dropdown menu will show a list of teams and the input boxes will display the names of the Captain and the Vice-Captain once the team is selected (actually it will only capture their MemberID in the backend and save them in a table) 一旦选择了团队,下拉菜单将显示一个团队列表,输入框将显示队长和副队长的姓名(实际上,它只会在后端捕获其MemberID并将其保存在表格中)

I had been able to fetch the name of one Captain, using a Javascript function and Ajax. 我已经能够使用Javascript函数和Ajax来获取一位队长的名字。 That part is working fine. 那部分工作正常。

What I am after is getting the second captains name/id. 我要的是得到第二个队长的名字/身份证。

My HTML/PHP code is here: 我的HTML / PHP代码在这里:

 <script type="text/javascript" src="js/capt-dropdown.js"></script> ... <table> <?php $x = 0; ?> <tr style="background-color:#FFF9C4"> <td>Select Team: </td> <td><select name="teamcode" id="teamcode"> <option disabled selected value> - Select Team - </option> <?php $sql_select = mysqli_query($conn, "SELECT teamcode, team FROM team"); while ($row = mysqli_fetch_assoc($sql_select)){ echo '<option value = " '.$row['teamcode'].'"> '.$row['team'].' </option>'; } ?> </select> </td> </tr> <tr> <td>Captain 1: </td> <td><input type="text" name="captain1" id="captain1"></td> </tr> <tr> <td>Captain 2: </td> <td><input type="text" name="captain2" id="captain2"></td> </tr> </tr> </table> 

The JS file: capt-dropdown.js JS文件:capt-dropdown.js

 $(document).ready(function() { $("#teamcode").change(function() { var team = $(this).val(); if(team != "") { $.ajax({ url:"get-captain.php", data:{teamcode : team}, type:'POST', success:function(response) { var resp = $.trim(response); $("#captain1").html(resp); } }); } else { $("#captain1").html("<option value=''>--- Select ---</option>"); } }); }); 

The php file: get-captain.php is here: php文件:get-captain.php在这里:

 if(isset($_POST['teamcode'])) { $teamcode = trim($_POST['teamcode']); $sql_select = mysqli_query($conn,"SELECT surname, preferred_name, memberid FROM member WHERE memberid in ((SELECT captain FROM captain WHERE year = YEAR(CURDATE()) AND teamcode = '$teamcode'), (SELECT vcaptain FROM captain WHERE year = YEAR(CURDATE()) AND teamcode = '$teamcode'))"); while ($result = mysqli_fetch_assoc($sql_select)){ $name = ($result['surname']. ', '.$result['preferred_name']); echo '<option value = " '.$result['memberid']. '">'.$name.'</option>'; } } 

Member table contains the name, surname and member-id of members Captain table contains historic data of team captains Captain table 成员表包含成员的姓名,姓氏和成员ID队长表包含团队队长的历史数据队长表

The query in get-captain.php is working, and is able to retrieve the captain and vcaptain's names & ids. get-captain.php中的查询正在运行,并且能够检索船长和vcaptain的名称和ID。

Read several posts but couldn't find anything similar to what I am doing here. 阅读了几篇文章,但找不到与我在这里所做的相似的事情。

Can anyone please suggest how to fetch the second captains (the vice-captains) details? 任何人都可以建议如何获取第二队长(副队长)的详细信息吗? (ie. Display his name and capture his member-id) (即,显示他的名字并获取他的会员ID)

Pls Note: this is an extract of a form which contains lot of other information, hence my apologies if I missed a line while cutting & pasting here) 请注意:这是包含许多其他信息的表格的一部分,因此,如果我在此处剪切和粘贴时错过一行,我深表歉意。

Thank you in advance. 先感谢您。

Sorry I cannot comment yet due to my rep, but captain 1 & 2 are inputs why are you trying to echo out option tags and insert them inside captain1. 抱歉,由于我的代表,我无法发表评论,但是队长1和2是输入,为什么您要回显选项标签并将其插入队长1中。

You should change get-captain.php to fetch an associative array and echo json_encode($array); 您应该更改get-captain.php以获取关联数组并echo json_encode($array); Your javascript can then parse json array var array=JSON.parse(response); 然后,您的JavaScript可以解析json数组var array=JSON.parse(response); finally putting your array values inside your input 最后将数组值放入输入中

$("#captain1").html(arr[0]["surname"] + "," + arr[0]["preferred_name"]);
$("#captain1").val(arr[0]["memberid"]);
$("#captain2").html(arr[1]["surname"] + "," + arr[1]["preferred_name"]);
$("#captain2").val(arr[1]["memberid"]);

If your dealing with more than 2 captains then you can just loop through your array and add a counter. 如果您与两个以上的队长打交道,那么您可以遍历数组并添加一个计数器。 Hope this makes sense i've never been good at explaining. 希望我从来没有善于解释,这是有道理的。

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

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