简体   繁体   English

使用选择框中的值从数据库中提取记录

[英]Fecting records from the database using the value in a selection box

Please, help me out to solve the following problem. 请帮我解决以下问题。 I have a web page that takes some information from the user. 我有一个网页,可以从用户那里获取一些信息。 I have loaded all the Member IDs to a select box, which a member can select their own Member ID easily. 我已将所有成员ID加载到一个选择框中,该成员可以轻松选择自己的成员ID。 My problem is, I want to display automatically the first name and the Last name of the that particular user once his/her Member ID is selected. 我的问题是,一旦选择了他/她的会员ID,我想自动显示该特定用户的名字和姓氏。 I know that I require Javascript or Jquery with PHP and MySql, But I don't know how to use it properly. 我知道我需要Javascript或PHP和MySql的Jquery,但是我不知道如何正确使用它。 Part of the coding is as follows. 部分编码如下。

//loads Member ID to a select box //将会员ID加载到选择框

<?php
include('connectdb.php');
$sqlselect = "SELECT MemID FROM Member" ;
$result = mysqli_query($dbcon,$sqlselect);
<?

//displaying membr ID in select box //在选择框中显示膜ID

<html><head><title></title></head>
<body>
<form action="bookrequestmem.php" method="post"> 
<label for="memberid">Member ID:</label><br>
<select id="memid" name="memid1">  
            <option>Select Member ID</option>";
            <?php while($row = mysqli_fetch_array($result)):;?>
            <option><?php echo $row[0];?></option>";
            <?php endwhile;?>
 </select>
 <label for="fname">First Name:</label><br>
 <Input type="text" name="memfname"/>
 <label for="lname">Last Name:</label><br>
 <Input type="text" name="memlname"/>
 </form>
 </body>
 </html>

Please tell me how to disply the First name and the Last name automatically when the proper Member ID is selected. 请告诉我选择正确的会员ID后如何自动显示名字和姓氏。

    $(document).ready(function(){
            $("#memid").change(function(){
                var id = $("#memid").val();

                 $.ajax({
                            type: 'POST',
                            url: 'getname.php',
                            data: {id: id},
                            success: function (data) {
                                var name = data.split(" ");

                    $("#fname").val(name[0]);
                    $("#lname").val(name[1]);   
                            }

            });
 });

        });

Then in getname.php file use id posted by the ajax method and fetch the firstname and lastname of the member and echo them separated by space as we are using split on "space" and then set the values of the input fields. 然后,在getname.php文件中,使用ajax方法发布的ID,并获取成员的名字和姓氏,然后将它们回显为空格,因为我们在“ space”上使用split,然后设置输入字段的值。

Smart Way without AJAX 无需AJAX的智能方式

  function get_meminfo() { var dt=($("#memid").val()).split("~~"); $("#memfname").val(dt[1]); $("#memlname").val(dt[2]); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <html><head><title></title></head> <body> <form action="bookrequestmem.php" method="post"> <label for="memberid">Member ID:</label><br> <select id="memid" name="memid1" onChange="get_meminfo()"> <option>Select Member ID</option>"; <?php while($row = mysqli_fetch_array($result)):;?> <option value="MEM_ID~~FIRST_NAME~~LAST_NAME">Test MEM ID</option>"; <?php endwhile;?> </select> <label for="fname">First Name:</label><br> <Input type="text" name="memfname" id="memfname" /> <label for="lname">Last Name:</label><br> <Input type="text" name="memlname" id="memlname"/> </form> </body> </html> 

暂无
暂无

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

相关问题 如何根据使用选择框选择的值从数据库中检索特定记录? - How to retrieve a specific record from the database based on the value selected using a selection box? 如何通过使用javascript检查mysql数据库中的值是否存在,从选择下拉框中删除选项 - how to remove an option from selection drop down box by checking that the value exists in mysql database using javascript 如何使用jQuery从两个三维选择框中获取价值 - How to get value from two dimentional Selection Box using Jquery 在使用struts2选择一个下拉值时从数据库中检索值 - Retrieving values from database on selection a dropdown value using struts2 将从数据库检索到的记录显示在对话框中 - To display the records retrieved from database into a dialog box 使用Ajax,如何将表单中的下拉菜单选择值与数据库中的记录进行匹配,并获取关联的记录/行以预填表单? - Using Ajax how can I match a drop down menu selection value from a form to records in my database and fetch associated record/row to prefill a form? 如何从多重选择框控制多重选择以显示值 - how to control multi selection from multi selection box to display value 从下拉菜单中选择带有下拉菜单的数据库,而无需提交按钮 - Load records from database with dropdown selection without submit button 在jsp中选择<option>时从数据库中获取值 - fetch value from database on selection of <option> in jsp 如何基于数据库中的下拉菜单选择更改文本框值 - How to change text box values based on dropdown selection from database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM