我有index.php文件,它有一些PHP代码从MySQL数据库调用数据。
这些数据将以this等形式显示。
我的问题
我想加上每一行的值,并将其插入每行的 余额 输入中。
喜欢:1 | 3 | 5 | 6 | 余额 = 15并添加每列的值并将其插入每列的 总计 输入中,如:
5
6
4
3
总计 18
有关详细信息,请参阅this图片。
我开发了这个jquery代码,但它正常工作
<script>
$(function () {
$('input[name="Budget[]"],input[name="Availed_in_Regions[]"]').on("change",function (){
var $container = $(this).closest('.form-group');
qty = Number($('input[name="Budget[]"]',$container).val())||0,
price = Number($('input[name="Availed_in_Regions[]"]',$container).val())||0;
$('input[name="Balance[]"]',$container).val(qty * price);
})
});
</script>
这是我的 index.php 文件
<?php
$con=mysqli_connect("127.0.0.1","root","","dji001");
$result = mysqli_query($con,"SELECT * FROM finance")
or die("Error: ".mysqli_error($con));
echo "<form style='width:1100px; backgrond-color:transparent;' action='update.php' method='post' class='form-group'>";
while($row = mysqli_fetch_array($result))
{
$Budget = $row['Budget'];
$Availed_in_Regions = $row['Availed_in_Regions'];
$Requested_in_KBL = $row['Requested_in_KBL'];
$Received_in_KBL = $row['Received_in_KBL'];
$Availed_in_KBL = $row['Availed_in_KBL'];
$Balance = $row['Balance'];
echo "<input type='hidden' name='id[]' value='".$row['ID']."'>
<input type='text' name='Budget[]' value='".$row['Budget']."'>
<input type='text' name='Availed_in_Regions[]' value='".$row['Availed_in_Regions']."'>
<input type='text' name='Requested_in_KBL[]' value='".$row['Requested_in_KBL']."'>
<input type='text' name='Received_in_KBL[]' value='".$row['Received_in_KBL']."'>
<input type='text' name='Availed_in_KBL[]' value='".$row['Availed_in_KBL']."'>
<input type='text' name='Balance[]' value='".$row['Balance']."'>";
}
echo "<input type='Submit' value='Submit'></form>";
?>
这是我的 update.php 文件
<?php
mysql_connect('127.0.0.1', 'root', '') or die(mysql_error());
mysql_select_db("dji001") or die(mysql_error());
$ud_id = $_POST['id'];
$ud_Budget = array_map('mysql_real_escape_string', $_POST['Budget']);
$ud_Availed_in_Regions = array_map('mysql_real_escape_string',$_POST["Availed_in_Regions"]);
$ud_Requested_in_KBL = $_POST["Requested_in_KBL"];
$ud_Received_in_KBL = $_POST["Received_in_KBL"];
$ud_Availed_in_KBL = $_POST["Availed_in_KBL"];
$ud_Balance = $_POST["Balance"];
foreach($ud_id as $key => $value){
$query = "UPDATE finance
SET
Budget = '$ud_Budget[$key]',
Availed_in_Regions = '$ud_Availed_in_Regions[$key]',
Requested_in_KBL = '$ud_Requested_in_KBL[$key]',
Received_in_KBL = '$ud_Received_in_KBL[$key]',
Availed_in_KBL = '$ud_Availed_in_KBL[$key]',
Balance = '$ud_Balance[$key]'
where ID = $value";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=0){
echo "<p>Record Updated<p>";
}else{
echo "<p>Not Updated<p>";
}
}
?>
答案 0 :(得分:0)
您应该在INPUT元素周围添加一个容器标记,并为每个元素添加一个类。
echo "<div class='calc_container'>
<input type='hidden' class='id' name='id[]' value='".$row['ID']."'>
<input type='text' class='budget' name='Budget[]' value='".$row['Budget']."'>
<input type='text' class='avail_region' name='Availed_in_Regions[]' value='".$row['Availed_in_Regions']."'>
<input type='text' class='req_kbl' name='Requested_in_KBL[]' value='".$row['Requested_in_KBL']."'>
<input type='text' class='rec_kbl' name='Received_in_KBL[]' value='".$row['Received_in_KBL']."'>
<input type='text' class='avail_kbl' name='Availed_in_KBL[]' value='".$row['Availed_in_KBL']."'>
<input type='text' class='balance' name='Balance[]' value='".$row['Balance']."'>
</div>";
然后使用jQuery并对输入文本元素的change事件进行计算。
jQuery('.id, .budget, .avail_region, .req_kbl, .rec_kbl, .avail_kbl').change(function(){
var all_inputs = jQuery(this).parent('.calc_container').find("input[class!='balance']");
var total = 0;
jQuery.each( all_inputs, function(value){
total += parseInt( jQuery(value).text(), 10 );
});
jQuery(this).parent('.calc_container').find("input.balance").text(total);
});
此代码缺少一些数字验证,但应根据您的需要进行操作。