[英]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.