[英]I want to display a name based on the id select in dynamic dropdown list
I want to display name in textbox based on the selected id.我想根据选定的 id 在文本框中显示名称。 This id to be dynamically displayed in dropdown list, so while select it the name will displayed from the db table.
此 id 将动态显示在下拉列表中,因此 select 它的名称将从 db 表中显示。
HTML code: HTML 代码:
<form name="insert" action="" method="post">
<table width="100%" height="117" border="0">
<tr>
<th width="27%" height="63" scope="row">ID :</th>
<td width="73%"><select onChange="getdistrict(this.value);" name="state" id="state" class="form-control" >
<option value="">Select Id</option>
<?php $query =mysqli_query($con,"SELECT * FROM patreg");
while($row=mysqli_fetch_array($query))
{ ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<th scope="row">Name :</th>
<td>
<input type="text" id="district-list" class="form-control" value="<?php echo $row["name"]; ?>">
</td>
</tr>
</table>
</form>
<?php
require_once("config.php");
if(!empty($_POST["name"]))
{
$query =mysqli_query($con,"SELECT `name` FROM `patreg` WHERE `id` = '$id'");
?>
<option value="">Select District</option>
<?php
while($row=mysqli_fetch_array($query))
{
?>
<?php echo $row["name"]; ?>
<?php
}
}
?>
ID = 100001 while selected name = abcdef to be displayed automatically ID = 100001 而选择的名称 = abcdef 将自动显示
<script>
function getdistrict(val) {
$.ajax({
type: "POST",
url: "get_district.php",
data:'id='+val,
success: function(data){
$("#district-list").html(data);
}
});
}
function selectCountry(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
Assuming that the ajax request is correctly sending the request to get_district.php
with the parameter id
rather than name
as your code looks for then perhaps the following might help?假设 ajax 请求正确地将请求发送到
get_district.php
参数id
而不是您的代码查找的name
,那么以下可能会有所帮助? The original code is vulnerable to SQL injection so you would be advised to use a prepared statement
- the following has not been tested so excuse silly mistooks...原始代码容易受到 SQL 注入的影响,因此建议您使用
prepared statement
- 以下内容尚未经过测试,所以请原谅愚蠢的错误......
<?php
# get_district.php
require_once("config.php");
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['id'] ) {
$id=$_POST['id'];
$sql='select `name` from `patreg` where `id` =?';
$stmt=$con->prepare( $sql );
$stmt->bind_param( 's', $id );
$result=$stmt->execute();
if( $result ){
$stmt->store_result();
$stmt->bind_result( $name );
echo '<option selected hidden disabled>Please select District';
while( $stmt->fetch() ){
printf( '<option value="%1$s">%1$s', $name );
}
$stmt->free_result();
}
$stmt->close();
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.