在php中选择选项时设置输入值 [英] Set input value upon select option in php
本文介绍了在php中选择选项时设置输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张表格供我选择产品。在选定产品的基础上,我想更新表格中剩余的字段。
在选择产品名称时,我希望剩下的
以下是创建表的代码。
< table id =productTableclass =table-c>
< tr>
< th class =text-centerstyle =width:5%;> SR No.< / th>
< th class =text-centerstyle =width:45%> DESCRIPTION< / th>
< th class =text-centerstyle =width:10%> HSN / SAC< / th>
< th class =text-centerstyle =width:10%> QTY IN-HAND< / th>
< th class =text-centerstyle =width:10%> ENTER OUTWARD QTY< / th>
< th class =text-centerstyle =width:5%>删除< / th>
< / tr>
< tr style =text-align:center; ID = 产品 >
<?php $ j = 0; $ J ++; ?>
< td><?php echo $ j; ?>< / TD>
< td>< select class =form-controlname =codeid =productIDstyle =width:429px;>
<?php
$ sql =SELECT * FROM`product`;
$ result = $ conn-> query($ sql);
if($ result-> num_rows> 0){
//每行输出数据
while($ row = $ result-> fetch_assoc()) {
echo'< option id ='。$ row ['code']。'value ='。$ row ['pname']。'>。$ row ['pname' ]。 < /选项> 中;
}
} else {
echo0 results;
}
?>
< / select>
< / td>
< td>< input type =numbername =hsnNoid =hsnNoreadonly>< / td>
< td>< input type =numbername =qtyid =qtyreadonly>< / td>
< td class =coljoin>< input type =numberformat =2name =amount>< / td>
< td>
< span class =fa fa-trash>< / span>
< / td>
< / tr>
< / table>
我应该怎么做?
解决方案
这是PHP代码: -
code><形式>< option value => - 选择客户名称 - < / option>
< option value =1> John< / option>
< option value =2> Smith< / option>
< / select>
地址:< input name =addtype =textvalue =>
手机:< input name =mobiletype =textvalue =>
EMail-Id:< input name =emailtype =textvalue =>
< / form>
这是JS代码: - $ /
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js>< / script>
< script>
$(document).ready(function(){
$(#customer_id)。change(function(){
var id = $(this).val();
var dataString ='cid ='+ id;
// alert(dataString);
$ .ajax({
url:'dataAjax.php',
type :'post',
data:dataString,
success:function(response){
//解析返回的json
var Vals = JSON.stringify (response);
//这些是将填充
$(input [name ='add'])。val(Vals.add);
$(input val(Vals.mobile);
$(input [name ='email']).val(Vals.email);
} $ b $('name ='mobile']) b});
});
});
< / script>
这是其他PHP文件代码: - $ /
<?php
//这是您要执行任何数据库调用的位置
if(!empty($ _ POST)){
//通过echo
echo json_encode(array(add=>'India',mobile=>'1234567890','email'=>'demo@demo.com')发送一个jSON数组。 );
}
?>
I have a form where I am selecting a product. On the basis of the selected product, I want to update the rest of the fields in the form.
The Form display with select option
On selecting the product name, I want rest of the details of the form to fill up from the database using php.
Here is the code for the table created.
<table id="productTable" class="table-c">
<tr>
<th class="text-center" style="width: 5%;">SR No.</th>
<th class="text-center" style="width: 45%">DESCRIPTION</th>
<th class="text-center" style="width: 10%">HSN/SAC</th>
<th class="text-center" style="width: 10%">QTY IN-HAND</th>
<th class="text-center" style="width: 10%">ENTER OUTWARD QTY</th>
<th class="text-center" style="width: 5%">Delete</th>
</tr>
<tr style="text-align: center;" id="products">
<?php $j=0; $j++; ?>
<td><?php echo $j; ?></td>
<td><select class="form-control" name="code" id="productID" style="width: 429px;">
<?php
$sql = "SELECT * FROM `product`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option id='".$row['code']."' value='".$row['pname']."'>".$row['pname']."</option>";
}
} else {
echo "0 results";
}
?>
</select>
</td>
<td><input type="number" name="hsnNo" id="hsnNo" readonly></td>
<td><input type="number" name="qty" id="qty" readonly></td>
<td class="coljoin"><input type="number" format="2" name="amount"></td>
<td>
<span class="fa fa-trash"></span>
</td>
</tr>
</table>
How should I do this?
解决方案
This is PHP Code:-
<form>
<select name="customer" id="customer_id">
<option value="">-- Select customer Name -- </option>
<option value="1">John</option>
<option value="2">Smith</option>
</select>
Address: <input name="add" type="text" value="">
Mobile: <input name="mobile" type="text" value="">
EMail-Id:<input name="email" type="text" value="">
</form>
This is JS Code:-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#customer_id").change(function() {
var id = $(this).val();
var dataString = 'cid='+id;
//alert(dataString);
$.ajax({
url: 'dataAjax.php',
type: 'post',
data: dataString,
success: function(response) {
// Parse the jSON that is returned
var Vals = JSON.stringify(response);
// These are the inputs that will populate
$("input[name='add']").val(Vals.add);
$("input[name='mobile']").val(Vals.mobile);
$("input[name='email']").val(Vals.email);
}
});
});
});
</script>
This is other PHP File Code:-
<?php
// This is where you would do any database call
if(!empty($_POST)) {
// Send back a jSON array via echo
echo json_encode(array("add"=>'India',"mobile"=>'1234567890','email'=>'demo@demo.com'));
}
?>
这篇关于在php中选择选项时设置输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文