Php 我想根据在动态下拉列表中选择的id显示名称

Php 我想根据在动态下拉列表中选择的id显示名称,php,Php,我想根据所选id在文本框中显示名称。此id将在下拉列表中动态显示,因此在选择它时,名称将从db表中显示 HTML代码: <form name="insert" action="" method="post"> <table width="100%" height="117" border="0"> <tr> <th width="27%" height="63" scope="row">ID :</th>

我想根据所选id在文本框中显示名称。此id将在下拉列表中动态显示,因此在选择它时,名称将从db表中显示

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
姓名:

假设ajax请求使用参数
id
而不是代码所寻找的
name
正确地将请求发送到
get\u district.php
,那么下面可能会有所帮助?原始代码易受SQL注入攻击,因此建议您使用
准备好的语句
-以下内容尚未测试,请原谅愚蠢的错误

<?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();
    }

?>

<?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();
    }

?>