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