本文介绍了如果下拉值更改,则从数据库中获取值并填充所有文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据下拉值选择填充所有文本框值.我还通过 SQL 查询填充下拉值.这是 HTML 代码:
I am trying to fill all textbox value depend on dropdown value selection. And I also fill dropdown value by SQL query. Here is HTML Code:
<select name="name" ID="name" class="form-control">
<option value="Select">Select</option>
<?php
$qu="Select DISTINCT Cname from Client_table";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>
<label>Address</label><input type="text" name="add"/>
<label>Contact</label><input type="text" name="con"/>
数据库:
Client_table
C_no Cname Caddress Ccontact
1 Mohit xyz 0123645789
2 Ramesh abc 7485962110
在这里我很困惑如何根据下拉选择查找客户的地址和联系方式并使用此值填充文本框
Here I am confused that how to find address and contact of a client based on dropdown selection and fill textbox with this values
推荐答案
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname="stack";
// Create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
$qu = "SELECT DISTINCT Cname,Caddress,Ccontact FROM Client_table";
$res = $conn->query($qu);
?>
<select name="name" ID="name" onchange="myFunction()" class="form-control">
<option value="Select">Select</option>
<?php
while($r = mysqli_fetch_row($res))
{
echo "<option data-add='$r[1]' data-con='$r[2]' value='$r[0]'> $r[0] </option>";
}
?>
</select>
<label>Address</label><input type="text" name="add" id="add"/>
<label>Contact</label><input type="text" name="con" id="con"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function myFunction(){
var address = $('#name').find(':selected').data('add');
var contact = $('#name').find(':selected').data('con');
$('#add').val(address);
$('#con').val(contact);
}
</script>
这篇关于如果下拉值更改,则从数据库中获取值并填充所有文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!