如何访问动态生成的值?

时间:2014-08-10 04:01:51

标签: php jquery dynamic-values

我有以下情况: 有一个发票构建器,其中输入数量和费率,并动态计算最终费率。除此之外,还可以选择添加更多字段。因此可以输入更多项目。现在我必须提供一个" Preview"发票的选项,以便最终可以将此发票另存为pdf。

以下是所用表单的html代码:

<form action = "display-invoice.php" name="invoice" id="invoice" data-parsley-validate method="post" enctype="multipart/form-data" >
<table>
<tr>
<td>Party Name </td><td> <input name="party_name" type="text" required id="party_name" placeholder="Party Name"/></td>
</tr>
<tr>
<td>Address</td><td><input name="party_address" type="text" required id = "party_address" placeholder="Enter Address"/></td>
</tr>
<tr>
<td>Invoice Number</td>
<td><?php include('connect.php');
$dbconn = new connect();
$rand_no = $dbconn->get_rand_no(10); 
echo $rand_no;?>
</td>
<input type="hidden" name="invoice_number" value="<?php echo $rand_no;?>"> 
</tr>
</table>
<table>
<thead>
<tr>
<th>Item Name</th><th>Qunatity</th><th>Rate</th><th>Final Rate</th>
</tr>
<tbody id="invtbody">
        <tr>
        <td><input type="text" name="item_name[]" id ="item_name1" placeholder="Item Name" class="itemname" refid="1" required/></td>
        <td><input type="text" name="item_qty[]" id ="item_qty1" placeholder="Quantity" class="itemqty"  refid="1" required/></td>
        <td><input type="text" name="item_rate[]" id ="item_rate1" placeholder="Rate" class="itemrate"  refid="1" required/></td>
        <td><input type="text" name="final_rate[]" id="final_rate1"  refid="1" class="itemfinal" disabled/></td>
        </tr>
</tbody>
<tr>
<td></td><td></td>
<td>GrandTotal </td><td><div id="totalamount"></div></td>
</table>

<div id="add"></div><br>
<input type="button" value="ADD MORE" id="addBtn" ><br>
<input type="submit" name="preview-invoice" value="PREVIEW" >  
<input type="hidden" name="totalinputs" value="1">
<input type="hidden" name="total_amount[]" id="total_amount" value="0" >
</form>

这是计算最终费率和总计的代码:

$(".itemrate,.itemqty").live('keyup',function(){
        var refid = $(this).attr('refid');
        var qty = "#item_qty"+refid;
        var rate = "#item_rate"+refid;
        var finalrate = "#final_rate"+refid;
        var totalinpval = $("input[name=total_amount]").val();
        if(($(qty).val() != "") && ($(rate).val() != ""))
        {
        var total = parseInt($(qty).val()) * parseFloat($(rate).val());
        $(finalrate).val(total);
        $("#total_amount").val(total); //tried putting the value in the hidden value but this doesnt work too!
        var grandtotal = 0;
        $(".itemfinal").each(function(){
                grandtotal += parseInt($(this).val());
            }); 
                $("#totalamount").html(grandtotal);

        } 

ajax调用看起来像这样

$("input[name=preview-invoice]").click(function(){
        $.ajax({
            type:'POST',
            url:'display-invoice.php',
            data: $("#invoice").serialize(),
            success: function(msg){}
            })
        })

在display-invoice.php中,我显示的是这样的声明:

<table border="2" bgcolor="#50D5CE">
<tr>
<th>Item Name</th><th>Quantity</th><th>Rate</th><th>Final Rate</th>
</tr>
<?php $totalcount = $_REQUEST['totalinputs'];
for($i=0;$i<$totalcount;$i++){ ?>
<tr>
<td><?php echo $_REQUEST['item_name'][$i]; ?></td>
<td><?php echo $_REQUEST['item_qty'][$i]; ?></td>
<td><?php echo $_REQUEST['item_rate'][$i]; ?> </td>
<td><?php echo $_REQUEST['final_rate'][$i]; ?></td>
</tr>
<?php
}
?>
<tr>
<td></td><td><td>Grandtotal</td><td><?php ?></td>
</tr>
</table>

我得到的错误是Undefined Index:final_rate。 请帮忙!

1 个答案:

答案 0 :(得分:0)

这不是一个错误,这只是一个未定义的索引,我认为它只是一个&#34;注意&#34;,你可以解释它:

echo (isset($_REQUEST['final_rate'][$i])) ? $_REQUEST['final_rate'][$i] : "";