简体   繁体   English

我想在动态下拉列表中显示基于 id select 的名称

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

相关问题 当我单击客户名称表单下拉列表时,我想显示数据,它将仅显示该客户的消息和输入日期 - I want to display data when i click on customer name form dropdown list it will display only that customer's message and entry date PHP根据动态下拉列表选择默认下拉列表 - Php select default dropdown based on dynamic dropdown 我希望html的ID和名称选择具有不同的值 - I want ID and Name of html select to have different value 我想从一个表中选择名称并显示在我的页面中 - i want select name form one table and display in my page 在我的Yii2下拉列表中,我需要将“ id”列作为选项的值,并将name列作为用户在select选项中看到的内容 - In my Yii2 dropdown list I need my 'id' column to be the value of my options and the name column to be what the user sees in the select option Codeigniter的form_dropdown用于在下拉列表中显示类别名称,但插入类别ID - Codeigniter's form_dropdown to display category name in dropdown list but insert category id 我想在laravel的Select Options菜单中显示国家的完整列表 - I want to display the entire list of countries in the Select Options menu in laravel 我想根据下拉选择列表显示数据 - I want to display the data based on drop down selection list 动态下拉列表根据答案显示唯一文本 - dynamic dropdown display unique text based on answer 我想使用 jquery 获取类名的 id 我的类名是动态的,并且正在从 url 中获取它 - I want to get id of the class name using jquery my class name is dynamic and am getting it form url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM