How to access values which are dynamically generated?

423 Views Asked by At

I have the following scenario: There is an invoice builder where the quantity and rate is entered and the final rate is calculated dynamically. Along with this, there is an option of adding more fields. So more items can be entered. Now I have to provide a "Preview" option to for invoice, so that eventually this invoice can be saved as a pdf.

Below is the html code for the form used:

<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>

And this is the code where final rate and grand total are being calculated:

$(".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);

        } 

The ajax call looks like this

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

In display-invoice.php I am displaying vlaues like this:

<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>

The error I get is Undefined Index: final_rate. Please help!

1

There are 1 best solutions below

1
On

That's not exactly an error, that is just an undefined index which I believe is just a "Notice", you can account for it like:

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