This is a table and i wrote a jquery to add more rows and delete rows. then i want to add this data to database as a array(1 array = 1 row). but here i cant get values. i used this code to see the output. print_r($_POST['list']);
and the output is this one.
Array
(
[0] => Array
(
[id] => 123
)
[1] => Array
(
[name] => lakith
)
[2] => Array
(
[id] => 456
)
[3] => Array
(
[name] => lahiru
)
)
But i want this type of out put. in here i input two raws. and i want to get two arrays. but according to html table in below i cant get this. the output what i can get is the above one
Array
(
[0] => Array
(
[id] => 123
[name] => lakith
)
[1] => Array
(
['id'] => 456
['name'] => lahiru
)
)
This is my html page:
<form action="post.php" method="POST">
<table id="tb1">
<tr>
<td> <input type="text" name="list[][i']" placeholder="1"></td>
<td> <input type="text" name="list[][name]" placeholder="2"></td>
<td><input type='button' class='AddNew' value='Add new item'></td>
</tr>
</table
</br>
<input type="submit" id="submit" name="submit" value="Register">
</form>
In here i wrote it to add some rows to table and delete that rows if i don't want.
Can u guys give me the solution? i tries to get a solution to fix this. but couldn't to find way to correct this. so please help me to correct this one.
Your code is adding each field as a new array element to list
:
<tr>
<td> <input type="text" name="list[][id]" placeholder="1"></td>
<!-- ---------------------------- ^^ -->
<td> <input type="text" name="list[][name]" placeholder="2"></td>
<!-- ---------------------------- ^^ -->
<td><input type='button' class='AddNew' value='Add new item'></td>
</tr>
To organize the array like you're asking, you need to do something like this:
<tr>
<td> <input type="text" name="list[row_one][id]" placeholder="1"></td>
<!-- -------------------------------- ^^ -->
<td> <input type="text" name="list[row_one][name]" placeholder="2"></td>
<!-- -------------------------------- ^^ -->
<td><input type='button' class='AddNew' value='Add new item'></td>
</tr>
<tr>
<td> <input type="text" name="list[row_two][id]" placeholder="1"></td>
<!-- -------------------------------- ^^ -->
<td> <input type="text" name="list[row_two][name]" placeholder="2"></td>
<!-- -------------------------------- ^^ -->
<td><input type='button' class='AddNew' value='Add new item'></td>
</tr>