• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Submitting values of "jQuery Editable Invoice" into a MySQL DB

#1
[eluser]Timothy_[/eluser]
Hello Everyone,

I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.

It's working quite well and I am successfully generating the items using multiple foreach loops but I now need to save the values entered into the various text fields and enter them into a new table in the MySQL database.

I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB. It also needs to look for newly added 'fields' added by the user dynamically with jquery.

Here is an example of my code that I am using to generate the items:
Code:
<?php if($invoice_items->result_array()) { ?>
    <?php foreach($invoice_items->result_array() as $invoice_Row): ?>
        <tr class="item-row">
                  <td class="item-name">
                <div class="delete-wpr">
                    &lt;textarea&gt;&lt;?php echo $invoice_Row['item_name']; ?&gt; Facility Booking&lt;/textarea&gt;
                    <a class="delete" href="[removed];" title="Remove row">X</a>
                </div>
            </td>
                  <td class="description">
                      &lt;textarea&gt;&lt;?php echo $invoice_Row['description']; ?&gt;&lt;/textarea&gt;
                  </td>
                  <td>&lt;textarea class="cost"&gt;$&lt;?php echo $invoice_Row['hourly_cost']; ?&gt;.00&lt;/textarea&gt;&lt;/td>
                  <td>&lt;textarea class="qty"&gt;&lt;?php echo $total_time_hours; ?&gt;&lt;/textarea&gt;&lt;/td>
                  <td><span class="price">$&lt;?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?&gt;.00</span></td>
        </tr>
    &lt;?php endforeach; ?&gt;
    &lt;?php } ?&gt;
I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie `item-1-desc`, `item-1-cost` etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...

If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.

Thanks,

Tim

#2
[eluser]georgeedwards[/eluser]
Hey there not sure if you've already sorted this, but I've done something similar before.

I think I can see what you need to do. In my case, I named the HTML form elements with arrays. For example you could do in the view:

Code:
&lt;form type="text" name="item[1][desc]" /&gt;
&lt;form type="text" name="item[1][cost]" /&gt;

&lt;form type="text" name="item[2][desc]" /&gt;
&lt;form type="text" name="item[2][cost]" /&gt;

&lt;form type="text" name="item[3][desc]" /&gt;
&lt;form type="text" name="item[3][cost]" /&gt;

...

Then in your controller, you will receive multi-dimensional arrays of form data, so you can manipulate like the following:

Code:
foreach ($_POST['item'] as $item) {
     // Logic here...
     // $item['cost'] and $item['desc'] are accessible
}

Is this any help to you at all?

Post back if you need any more help, I may be able to help you with the Javascript side of things too (or at least I'll do my best!).

Cheers


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.