![](/img/trans.png)
[英]How to select data from database and sum the column of the row selected also display in page using 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
在下拉選擇更改事件中,如何從輸入字段中的數據庫中獲取選定數據?
我在同一頁面中使用html
和PHP
。 這意味着當我從下拉列表中更改選定的值時,我輸入字段中的數據也應更改,這些數據來自數據庫。
我想在選定的更改事件上觸發PHP
數據庫查詢。
我想要php調用的代碼。 表示當我在按鈕上調用它時,我將使用以下代碼:if(isset($ _ POST ['submitbill']))。 如何在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'];
}}
?>
我已經添加了這段代碼,現在對我來說很好用,但是它會將整個表單復制到同一表單上
您必須為下拉菜單創建一個on change事件。 發生更改時,將所選下拉項的ID捕獲到變量中。
之后,您需要獲取與該ID相關的數據。 因此,您需要創建一個ajax get請求以從php文件中獲取數據。
然后成功執行php get請求,並使用數據填充您的輸入字段。
您可以使用jquery輕松完成此操作。
$("#dropdownid").change(function () {
var currentId= this.value;
$.ajax({
type: "GET",
url: "action.php",
data: {
currentId: currentId
},
success: function (data) {
alert(data);
}
});
});
在action.php文件中,創建獲取值所需的數據庫查詢,並將該currentId傳遞給查詢以獲取與其關聯的數據。
我希望這能幫到您 :)
附言:您的php,html和js位於同一文件中。 因此,無需調用極端的php文件(action.php),就需要調用您所在的同一文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.