douqiang4501 2018-10-09 18:20
浏览 166

如何在同一页面中使用html和php选择下拉列表更改事件的输入字段中从数据库中获取所选数据

How to get selected data from database in input fields on select change event of drop down?

I am using html and PHP in the same page. This means that when I change the selected value from drop down, my data in input field should also be changed which is coming from database.

I want to trigger PHP database query on selected change event.

i want code for php call. means when i am calling it on button then i will use this code: if(isset($_POST['submitbill'])). how to trigger it in php for select on change

 $(function(){
      $("select[name='selectname']").change(function () {
      var str = "";
      $("select[name='selectname'] option:selected").each(function () {
            str += $(this).text() + " ";

          });

            jQuery.ajax({
            type: "POST",
            data:  $("form#a").serialize(),

            success: function(data){
                jQuery(".res").html(data);

                $('#test').html(data);


            }
            });  
            var str = $("form").serialize();
            $(".res").text(str);
    });
    });


           <select name="selectname" id="selectname" class="form-control" >
                                                     <option value="0">Please select</option>
                                                     <!--code for fetching customer names in dropdown-->
                                                     <?php
                                                        $query1 = "SELECT name FROM tb_customer";
                                                                             $result1 = mysql_query($query1, $con) or die(mysql_error($con));
                                                        while($row = mysql_fetch_array($result1)){
                                                            $name = $row["name"];
                                                        ?>
                                                     <option value="<?php if(isset($name)) echo $name;?>"><?php if(isset($name)) echo $name;?></option>
                                                     <?php
                                                        }
                                                        ?>
                                                     <!--****************************************************-->
                                                  </select>
                                               </div>


                                            <div id="test">
                          <input type="text" hidden name="custnametrial" id="custnametrial" value="<?php if(isset($custnametrial)) echo $custnametrial;?>">
                          <input type="text" hidden name="customer_code" id="customer_code" value="<?php if(isset($customer_code)) echo $customer_code;?>">
                          <input type="text" hidden name="agency_code" id="agency_code" value="<?php if(isset($agency_code)) echo $agency_code;?>">



                           <span style="color:#000066;">Name :</span> 
                           <input type="text"   name="custnametrial" disabled style="border:none;background-color:#dddddd;" value="<?php if(isset($custnametrial)) echo $custnametrial;?>">
                           <br>
                           <span style="color:#000066;">Address :</span>
                           <input type="text"   name="address" disabled style="border:none;background-color:#dddddd;" value="<?php if(isset($address)) echo $address;?>">

                          <br>
                           <span style="color:#000066;">Pin Code : </span>
                          <input type="text"   name="pincode" disabled style="border:none;background-color:#dddddd;" value="<?php if(isset($pincode)) echo $pincode;?>">

                          <br>
                           <span style="color:#000066;">GSTIN : </span>
                           </span> <input type="text" disabled  name="gstin" style="border:none;background-color:#dddddd;" value="<?php if(isset($gstin)) echo $gstin;?>">

                          <br>
                          <span style="color:#000066;">State : </span>
                          <input type="text"  disabled name="state" style="border:none;background-color:#dddddd;" value="<?php if(isset($state)) echo $state;?>">

                          <br>
                           <span style="color:#000066;">Contact :</span>
                          <input type="number" disabled  name="contact_number" style="border:none;background-color:#dddddd;" value="<?php if(isset($contact_number)) echo $contact_number;?>">

                          <br>
                           <span style="color:#000066;">Email :</span>
                          <input type="email" disabled   name="email" style="border:none;background-color:#dddddd;" value="<?php if(isset($email)) echo $email;?>">
                          </div>


<?php

                                               if(isset($_POST['selectname']))                              
                                               { 

                                                    $name2 = $_POST['selectname'];                    

                                                $query1 = "SELECT * FROM tb_customer where name='$name2'";
                                                                            $result1 = mysql_query($query1, $con) or die(mysql_error($con));
                                                                            while($row = mysql_fetch_array($result1)){
                                                $custnametrial = $row['name'];
                                                $customer_code = $row['customer_code'];
                                                $address = $row['address'];
                                                $pincode= $row['pincode'];
                                                $gstin=$row['gstin'];
                                                $state=$row['state'];
                                                $contact_number=$row['contact_number'];
                                                $email=$row['email'];
                                                $bank_details=$row['bank_details'];

                                                                        }}


                                               ?>

I have added this code now working fine for me but its duplicating whole form on the same form

  • 写回答

1条回答 默认 最新

  • duanhuanzhi6431 2018-10-09 18:44
    关注

    You have to create an on change event to your dropdown. When a change happens grab the id of the selected dropdown item to a variable.

    After that you need to get the data associated to that id. So you need to create an ajax get request to get the data from a php file.

    Then in success of the php get request populate your input fields with the data.

    You can use jquery to done this easily.

    $("#dropdownid").change(function () {
        var currentId= this.value;
        $.ajax({
          type: "GET",
          url: "action.php",
          data: {
            currentId: currentId
          },
          success: function (data) {
            alert(data);
    
        }
       });
    });
    

    In action.php file create the database query you needed to get the values and pass that currentId to the query to get the data associated with it.

    I hope this will help you :)

    Ps: you have your php, html and js in the same file. So without calling to an extremal php file (action.php) , you need to call to the same file you're in.

    评论

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波