本文介绍了传递给PHP表单时,javascript动态变量的未定义索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< form action =testing.phpmethod =POSTid =addrecieptname =addreciept>
单元< input name =unit1type =textid =unit1size =17onkeyup =calc()value =0/>
Rate< input name =rate1type =textid =rate1size =17value =0onkeyup =calc()/>
金额<输入名称=amount1type =textid =amount1size =17disabled =true/>
Total Amount:< input type =textname =totalamountid =totalamountdisabled =truevalue =>
付款金额:< input type =textname =amount_paidid =amount_paidonkeyup =calc_balance()/>
余额:< input type =textname =balanceid =balancedisabled =truevalue =0>
< center>
< input type =submitvalue =Saveclass =button>
< / center>
< / form>
< script>
函数calc()
{
unit = document.getElementById('unit1')。value;
rate = document.getElementById('rate1')。value;
金额=单位*费率;
document.getElementById('amount1')。value = amount;
total_amount = document.getElementById('amount1')。value;
document.getElementById('totalamount')。value = total_amount;
}
函数calc_balance()
{
tota_amount = document.getElementById('totalamount').value;
paid_amount = document.getElementById('amount_paid')。value;
document.getElementById('balance')。value = tota_amount-paid_amount;
}
< / script>
我最近测试了在网上发布的脚本,它工作正常,但我想我做错了一些事情: '(我很困难,因为像amount1,totalamount和balance这样的动态变量导致了未定义的索引amount1等的错误等)。
plz计算出来: (
) php简单易懂>>
<?php
echo $ _POST ['unit1'];
echo $ _POST ['rate1'];
echo $ _POST ['amount1'];
?>
解决方案
form-elements在禁用时不会被提交(amount1,totalamount和平衡被禁用)
使用只读属性
<form action="testing.php" method="POST" id="addreciept" name="addreciept"> Unit<input name="unit1" type="text" id="unit1" size="17" onkeyup="calc()" value="0"/> Rate<input name="rate1" type="text" id="rate1" size="17" value="0" onkeyup="calc()" /> Amount<input name="amount1" type="text" id="amount1" size="17" disabled="true" /> Total Amount: <input type="text" name="totalamount" id="totalamount" disabled="true" value=""> Amount Paid: <input type="text" name="amount_paid" id="amount_paid" onkeyup="calc_balance()" /> Balance: <input type="text" name="balance" id="balance" disabled="true" value="0"> <center> <input type="submit" value="Save " class="button" > </center> </form> <script> function calc() { unit=document.getElementById('unit1').value; rate=document.getElementById('rate1').value; amount=unit*rate; document.getElementById('amount1').value=amount; total_amount=document.getElementById('amount1').value; document.getElementById('totalamount').value=total_amount; } function calc_balance() { tota_amount=document.getElementById('totalamount').value; paid_amount=document.getElementById('amount_paid').value; document.getElementById('balance').value=tota_amount-paid_amount; } </script>
I recently tested the script posted on net , it works, but I guess I am doing something wrong :'( i am badly stuck in it, because the dynamic variables like, amount1, totalamount & balance are causing error of undefined index amount1 etc etc
plz figure it out :(
php is just simple >>
<?php echo $_POST['unit1']; echo $_POST['rate1']; echo $_POST['amount1']; ?>
解决方案
form-elements will not be submitted when they are disabled(amount1,totalamount and balance are disabled)
Use the readonly-attribute instead
这篇关于传递给PHP表单时,javascript动态变量的未定义索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!