0

See the image below, I want to add many product as I want by clicking on "Add More Product" and then 2 new elements will be appear (Category and Items dropdowns). This will be done via jQuery to copy previous <li> and insert a new one.

When the elements are created, what the name of the dynamic fields should be? The data will be added into $_POST and I can insert into database.

enter image description here

HTML below, see dynamic fields name (example: name="items_2", name="items_2"): I have also added hidden input of current_added_products field. This tell me how many products currently added. Not sure if I need this?

<ul>
    <li>
        <label>Category</label>
        <select name="category_1"> 
            <option>CPU</option>
            <option>HDD</option>
        </select>
        <select name="items_1"> 
            <option>Item 1</option>
            <option>Item 2</option>
            <option>Item 3</option>
        </select>
    </li>
    <li>
        <label>Category</label>
        <select name="category_2"> 
            <option>CPU</option>
            <option>HDD</option>
        </select>
        <select name="items_2"> 
            <option>Item 1</option>
            <option>Item 2</option>
            <option>Item 3</option>
        </select>
    </li>
    <li>
        <label></label>
        <input type="hidden" name="current_added_products" value="2" />
        <input type="button" class="add_more_product" value ="Add More Product" />
    </li>
</ul>

Also, before the elements are created how to get the next numbers to be added?

I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213

5 Answers5

2

Try this:

$('.add_more_product').click(function() {
    var $ul = $('ul:last');
    $ul.clone().find('select').attr('name', function(i, cur) {
        return cur.match(/\D+/g).join('') + (Number(cur.match(/\d+/g).join('')) + 1)
    }).end().insertAfter($ul)
})​

http://jsfiddle.net/L7zwn/


$('.add_more_product').click(function(e) {
    e.preventDefault();
    var $ul = $('ul:last');
    $ul.clone().insertAfter($ul)
})

http://jsfiddle.net/U7Zeb/

Ram
  • 143,282
  • 16
  • 168
  • 197
1

You could give them all a name of category[] and items[], then when they are posted you'll end up with a numerically indexed array, this way you don't need to worry about appending a number to each newly created element. For example selecting CPU with Item 1 and HDD with Item 2 would result in the following once posted:-

array
  'category' => 
    array
      0 => string 'CPU' (length=3)
      1 => string 'HDD' (length=3)
  'items' => 
    array
      0 => string 'Item 1' (length=6)
      1 => string 'Item 2' (length=6)

This will make it a lot easier to add new form elements:-

html

<ul>
    <li>
        <label>Category</label>
        <select name="category[]">
            <option>CPU</option>
            <option>HDD</option>
        </select>
        <select name="items[]">
            <option>Item 1</option>
            <option>Item 2</option>
            <option>Item 3</option>
        </select>
    </li>
</ul>

<input type="button" class="add_more_product" value ="Add More Product" />

jquery

$(document).ready(function() {
    $('.add_more_product').on('click', function() {
        $('li:last').clone().appendTo('ul');
    });
});

Here's a fiddle

billyonecan
  • 20,090
  • 8
  • 42
  • 64
0

You can use name="item[1]", name="item[2]". This will result in an array in your $_POST['item'].

jgroenen
  • 1,332
  • 1
  • 8
  • 13
0

You can replace your html you got from copy using following code

htmlCopy.replace(/category_[0-9]+/, function(a){
       return "category_"+ (current_added_products+1);
});
htmlCopy.replace(/items_[0-9]+/, function(a){
       return "items_"+ (current_added_products+1);
});
Anoop
  • 23,044
  • 10
  • 62
  • 76
-1

use aray name as described in above answer. more to add in the databases use code like,

if (!$con){
   die('Could not connect: ' . mysql_error());
}

mysql_select_db($database, $con);

$authors = $_POST['Author'];
foreach($authors as $author) :
$sql="INSERT INTO savetest (type, number) VALUES ('{$_POST['type']}','{$author}')";

if (!mysql_query($sql,$con)){
   die('Error: ' . mysql_error());
}
endforeach;

more this post can help you..

Community
  • 1
  • 1
Mansoor Jafar
  • 1,458
  • 3
  • 15
  • 31