Q:

Inputing as array in textbox group

I have created a textbox group using JavaScript i.e. on clicking Add button new set of textboxes will be added to the page.
var newTextBoxDiv = $(document.createElement('div'))
                 .attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html('<label>Track #'+ counter + ' : </label>' + 
               '<input type="text" name="textbox1[]' + counter + '" value="" >' + 
               '<input type="text" name="textbox2[]' + counter + '" value="" >' );

newTextBoxDiv.appendTo("#TextBoxesGroup");

DIV class for my PHP page is..
    <div id='TextBoxesGroup'> <div id="TextBoxDiv1"> <label>Track #1 : </label>
    <input name="textbox1[]" type='textbox' ><input name="textbox2[]" type='textbox'>
    </div>
    </div>

Now I want to create an array so that, easily input values of this set of text boxes to database. Instead of using multidimensional array, I want to use single array so values of set of textbox1# stores as key for array and values of set of textbox2# stores as corresponding values for array.

A:

When you submit a form through POST, it doesn't make a multi-dimensional array of your input fields. Rather, it uses the name attribute of the field as the key in the $_POST array.
So when you're making the textboxes, make textbox2 an array name, so that you can have more than one field, like so:
var newTextBoxDiv = $(document.createElement('div'))
                 .attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html('<label>Track #'+ counter + ' : </label>' + 
               '<input type="text" name="textbox1[]' + counter + '" id="textbox1' + counter + '" value="" >' + 
               '<input type="text" name="textbox2[]' + counter + '" id="textbox2' + counter + '" value="" >' );

newTextBoxDiv.appendTo("#TextBoxesGroup");

Then you can get the keys using $_POST['textbox1'] and the values will be stored in $_POST['textbox2'].
Then to use it, let's say you wanted to get the first textbox1/textbox2 pair, you would access that value using $_POST['textbox1'][0] and $_POST['textbox2'][].
To loop through the two arrays and extract all of the values (assuming that the number of textbox1 and textbox2 fields are the same), you can do something like this:
$inputs;
$length = count($_POST['textbox1']);
for($i=0; $i< $length; $i++){
$inputs[$_POST['textbox1'][$i]] = $_POST['textbox2'][$i];
}

