简体   繁体   English

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

[英]how to get selected data from database in input fields on select change event of dropdown using html and php in the same page

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. 我在同一页面中使用htmlPHP 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. 我想在选定的更改事件上触发PHP数据库查询。

i want code for php call. 我想要php调用的代码。 means when i am calling it on button then i will use this code: if(isset($_POST['submitbill'])). 表示当我在按钮上调用它时,我将使用以下代码:if(isset($ _ POST ['submitbill']))。 how to trigger it in php for select on change 如何在php中触发以进行更改选择

 $(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 我已经添加了这段代码,现在对我来说很好用,但是它会将整个表单复制到同一表单上

You have to create an on change event to your dropdown. 您必须为下拉菜单创建一个on change事件。 When a change happens grab the id of the selected dropdown item to a variable. 发生更改时,将所选下拉项的ID捕获到变量中。

After that you need to get the data associated to that id. 之后,您需要获取与该ID相关的数据。 So you need to create an ajax get request to get the data from a php file. 因此,您需要创建一个ajax get请求以从php文件中获取数据。

Then in success of the php get request populate your input fields with the data. 然后成功执行php get请求,并使用数据填充您的输入字段。

You can use jquery to done this easily. 您可以使用jquery轻松完成此操作。

$("#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. 在action.php文件中,创建获取值所需的数据库查询,并将该currentId传递给查询以获取与其关联的数据。

I hope this will help you :) 我希望这能帮到您 :)

Ps: you have your php, html and js in the same file. 附言:您的php,html和js位于同一文件中。 So without calling to an extremal php file (action.php) , you need to call to the same file you're in. 因此,无需调用极端的php文件(action.php),就需要调用您所在的同一文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用php从数据库中选择数据并汇总所选行的列,该行也显示在页面中? - How to select data from database and sum the column of the row selected also display in page using php? 如何获取表单输入,从PHP的同一页中选择值? - how to get the form input, select values from the same page in php? 如何使用输入类型框从数据库中选择数据并在同一页面上打印 - how can i Select data from database using input type box and print on same page 如何使用下拉列表从数据库获取数据并显示到php mysql中的输入字段中? - How to get data from database using drop down list and display into input fields in php mysql? 如何使用php获取同一页面中的输入数据 - how to get input data in the same page using php 使用PHP从数据库中获取选定的下拉选项 - Get selected dropdown option from database using PHP 使用angularjs和php从数据库中选择的数据未显示到下拉列表中 - selected data from the database is not showing into dropdown list using angularjs and php 如何使用php从同一页上的sql select函数显示数据 - How to get data displayed from a sql select function on the same page using php php如何从数据库中创建所有表的下拉选择输入? - php how to create a dropdown select input of all table from a database? 如何从数据库中检索下拉按钮中的选定数据? - How to get selected data from dropdown button retrieve from database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM