![](/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.