简体   繁体   中英

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM