trullallero trullallero - 1 year ago 57
jQuery Question

Submit a dynamic table form through AJAX and get relatives values into PHP

I have a table form like this:

<table class="table table-striped table-bordered table-condensed">
<tbody>
<tr><td><input type="text" class="input-small" name="article" /></td>
<td>
<select name="colore">
<option value="nabuk">Nabuk</option>
<option value="nero">Nero</option>
<option value="blu">Blu</option>
<option value="rosso">Rosso</option>
</select>
</td>
<td>
<select name="fondo">
<option value="gomma">Gomma</option>
<option value="cuoio">Cuoio</option>
<option value="legno">Legno</option>
</select>
</td>
<td>
<select name="numero">
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
</select></td>
<td><input type="number" class="input-mini" min="1" max="200" name="qnt" step="1" /></td>
<td></td>
</tr>
</tbody>
</table>


The user can add many rows to this table with a Jquery script. This script is OK and work properly.
After that, the table form is submitted through an Ajax post call to a PHP page with the
$('form').serialize();
function of Jquery.

The problem is: how can I retrieve the values of each field of rows to the server side (PHP), without using hidden supplementary field (for example that submits the number of row in the table) or without use a progressive index for the name of the rows fields?

I haven't found something that explains the solution... :|

the next step of this script is passing the values of the table to the PHP with JSON object... but it's a secondary problem :)

Answer Source

You can use an array. Name your fields with [] at the end of them. For example,

<select name="colore[]">
...
<select name="fondo[]">
...
<select name="numero[]">

PHP will automatically update the indices as

$_POST['colore'][0]
$_POST['colore'][1]
$_POST['colore'][2]
etc.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download