0

I'm trying to pass multiple values from same parameters of a form using POST, but can't figure out how to proceed. I've used bootstrap css such that I can add multiple products. And I want to process the data of multiple orders by passing the values using POST method. On clicking 'Add another' link, additional set of data field appear which enables recording of multiple transactions of a same user. The code is as follows:

<div class="col-xs-5 col-lg-offset-3">



<form action="billingProceed.php" method="post" role="form">
<table id="itemElement">  
 <tr>
 <td>
<select class="form-control">

<option class="form-control"> Customer Name</option>
<option class="form-control"> Customer ID</option>
</select>
</td>
<td><input type="text" name="<?php echo $data["name"]; ?>" class="form-control"  /></td>
</tr>

 <tr>
 <td>
<select class="form-control">

<option class="form-control"> Item Name</option>
<option class="form-control"> Item ID</option>
</select>
</td>
<td ><input type="text" name="<?php echo $data["item"]; ?>" class="form-control"  /></td>
</tr>
 <tr>
 <td style="float:right;">Quantity
 </td>
 <td><input type="text" name="<?php echo $data["quantity"]; ?>" class="form-control"  /></td>
 </tr>
 <tr>
 <td style="float:right;">Price
 </td>
 <td><input type="text" name="<?php echo $data["price"]; ?>" class="form-control"  /></td>
 </tr>
 <tr>
 <td style="float:right;">Discount
 </td>
 <td><input type="text" name="<?php echo $data["discount"]; ?>" class="form-control"  /></td>
 </tr>
 </table>
<input type="submit" value="Proceed" class="btn btn-primary" />
<p style="float:right;"><a href="#" onclick="appendText()">Add another</a></p>
Kozzaja Man
  • 1
  • 1
  • 4

2 Answers2

2

You can use an array name.

Example:

<input name="data['name'][1]">
<input name="data['name'][2]">
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Ayush Ghosh
  • 487
  • 2
  • 10
0

Firstly you should be aware of the name array, for input names.

HTML Example:

<form>
   <a id="add_another" href="#">Add Another</a>
   <table>
       <tr class="product_item">
           <td>
               <input type="text" name="product[1][name]" value=""/>
           </td>
           <td>
               <input type="text" name="product[1][item]" value=""/>
           </td>
       </tr>
       <tr id="dummy">
          <td>
              <input type="text" name="product[0][name]" value=""/>
          </td>
          <td>
              <input type="text" name="product[0][item]" value=""/>
          </td>
       </tr>
   </table>
</form>

On POST, in your PHP script, you will access them as follows:

 foreach($_POST['product'] as $product)
 {
      $name = $product['name'];
      $item = $product['item'];
 }

Shoot for the JS

//You'll always start with one product row.
var productCount = 1;
$('#add_another').click(function() {
    var dummyproduct = $('#dummy').clone();
    //Increment Product count
    productCount += 1;

    //Rename all inputs
    dummyproduct.find('input[name^="product[0]"]').each(function(){
           $(this).attr('name',$(this).attr('name').replace('product[0]','product['+productCount +']'));
    });

    //Remove Id from cloned dummy
    dummyproduct.removeAttr('id').addClass('product_item');

    //Insert row before Dummy
    dummyproduct.insertBefore('#dummy');
});
Mysteryos
  • 5,581
  • 2
  • 32
  • 52
  • It would be easier if I could have a look at the JS code. Thanks in advance. – Kozzaja Man Dec 15 '14 at 11:07
  • Edited answer. This is a fairly simple 'add product' scenario. You should account for deletion of rows and when there are no rows left – Mysteryos Dec 15 '14 at 11:19
  • @Mysteryos Excuse me good sir, but could explain what the `^=` operator does? I just tried to google it and I am not finding anything, and I want to understand this code so that if I use it I can debug it. – Tyler Lazenby Jul 20 '18 at 15:14