我有一个php表单,我有四个字段,即Rate,Charged Rate,Excess和Charged Excess。现在,在这4个中,2个是不可编辑的(速率和超额),其余是可编辑的字段(费率和充电超额)。 现在,不可编辑字段中的值都是从表中回显php值,而可编辑的值也是如此。但唯一的区别是用户可以编辑较低的可编辑字段以更改输入的价格值。 第五个字段" Due_Amount"回应客户在应用一系列计算后必须支付的价值。
我想要的是,当编辑可编辑字段时," Due_Amount"具有从计算中回显的值的数据也会在输入时实时更改。 我写了一个javascript代码来做这件事,但由于某种原因,它是打印或做任何事情。这是js的代码。
function balance()
{
var bal = alert('<?php echo $balance; ?>');
console.log(bal);
}
function charge()
{
var rateCharged = alert('<?php echo $charge; ?>');
}
function excess()
{
var excess = alert('<?php echo $excess;?>')
}
function rateCharged()
{
var theForm = document.forms["returnform"];
var rateCharged = theForm.elements["rate_charged"];
var howmany = 0;
if(rate_charged.value!="")
{
howmany=parseInt(rate_charged.value);
}
return howmany;
}
function excessCharged()
{
var theForm = document.forms["returnform"];
var excessCharged = theForm.elements["charged_excess"];
var how_many = 0;
if(charged_excess.value!="")
{
how_many=parseInt(charged_excess.value);
}
return how_many;
}
function calculateTotal()
{
var balance = balance() + ((charge() + excess()) -(rateCharged() + excessCharged()));
console.log(balance);
document.getElementById('due_amount').value = balance;
//console.log(bal);
}
这里是php所谓的
<form action="return_book.php" id="returnform" method="post">
<?php
include 'connect_db.php';
include 'login_check.php';
$id1 = $_GET['id1'];
$id2 = $_GET['id2'];
//echo $id1;// bookid
//echo $id2; // user id
......其他代码的行
days = $diff->format("%a");
if($days > 0 and $due_date <= $date_in)
{
$excess = floor($EXCESS_CHARGE_RATE*$charge*$days);
$balance = $balance - $excess;
if ($balance >= 0)
{
$due_amount = 0;
}
else {
$due_amount = -1 * ($balance);
}
}
else {
$excess = 0;
if ($balance >=0)
{ $due_amount = 0 ;
}
else
{ $due_amount = 1 * ($balance);
}
}
书费率
<div class="controls">
<input type="text" id="charge" disabled value = "<?php echo $charge?>" placeholder=""><div style="color:red;" id = "paid_err">
</div>
</div>
<div class="control-group">
<label class="control-label" for="rate_charged">Rate Charged</label>
<div class="controls">
<input type="text" id="rate_charged" onchange="calculateTotal()" value = "<?php echo $charge?>" placeholder=""><div style="color:red;" id = "charged_rate_err">
</div>
</div>
<div class="control-group"></div>
<div class="control-group">
<label class="control-label" for="charge">Balance</label>
<div class="controls">
<input type="text" id="balance" disabled value = "<?php echo $balance?>" placeholder=""><div style="color:red;" id = "balance_err">
</div>
</div>
<div class="control-group"></div>
<div class="control-group">
<label class="control-label" for="excess"><div style="color:red">Excess</div></label>
<div class="controls">
<input type="text" id="excess" name = "excess" disabled value = "<?php echo $excess;?>" placeholder=""><div style="color:red;" id = "paid_err">
</div>
</div>
<div class="control-group">
<label class="control-label" for="excess_charged">Excess Charged</label>
<div class="controls">
<input type="text" id="charged_excess" name = "charged_excess" onchange="calculateTotal()" value = "<?php echo $excess;?>" placeholder=""><div style="color:red;" id = "charged_excess_err">
</div>
</div>
<div class="control-group"></div>
<div class="control-group">
<label class="control-label" for="">Due Amount</label>
<div class="controls">
<input type="text" name = "due_amount" id="due_amount" value = "<?php echo $due_amount?>" placeholder=""><div style="color:red;" id = "due_amount_err">
</div>
</div>
欢迎任何形式的帮助。我甚至试过查看控制台的一些错误的打印声明,但没有成功!我在前端没有看到任何东西