I am using jQuery serializeArray()
and it successfully collects the engine form info. However, I need the info in an array so that each "engine" is an item in the new array. Engine 1, Engine 2 etc. Below is my foreach
loop that delivers all the engines into the form (2 in this example), and then below that the output I get. How can I get that output in a new JSON array so I can iterate through each item? hope that makes sense.
<form id="engine_data">
<? foreach ($listing['engines'] as $k => $e) { ?>
<div id="engines_cont" >
<input type="hidden" name="engine_number" value="<?=$e['engine_no']?>" />
<table class="info_table engine_edit">
<colgroup>
<col span="1">
<col span="1" style="width: 100%;">
</colgroup>
<tbody>
<tr>
<td class="cell_label">MAKE</td>
<td class="cell_info"><input id="" name="engine_make" type="text" placeholder="Engine Make" value="<?=$e['engine_make']?>"></td>
</tr>
<tr>
<td class="cell_label">ENGINE TIME</td>
<td class="cell_info"><input id="" name="engine_time" type="text" placeholder="Engine Time" value="<?=$e['engine_time']?>"></td>
</tr>
<tr>
<td class="cell_label">ENGINE TBO</td>
<td class="cell_info"><input id="" name="engine_tbo" type="text" placeholder="Engine TBO" value="<?=$e['engine_tbo']?>"></td>
</tr>
<tr>
<td class="cell_label">ENGINE NOTES</td>
<td class="cell_info"><input id="" name="engine_notes" type="text" placeholder="Engine Notes" value="<?=$e['engine_notes']?>"></td>
</tr>
</tbody>
</table>
</div>
<? } ?>
</form>
[
{
"name": "engine_number",
"value": "1"
},
{
"name": "engine_make",
"value": "P&W"
},
{
"name": "engine_time",
"value": "1200"
},
{
"name": "engine_tbo",
"value": "2000"
},
{
"name": "engine_notes",
"value": "Great engine in good condition"
},
{
"name": "engine_number",
"value": "2"
},
{
"name": "engine_make",
"value": "P&W"
},
{
"name": "engine_time",
"value": "1500"
},
{
"name": "engine_tbo",
"value": "2200"
},
{
"name": "engine_notes",
"value": "Great engine in good condition"
}
]
2
Answers
Ids need to be unique so remove
id="engines_cont"
from your code or make it unique at least. (it seems to be no use in your code so you can remove it too)Now you need to loop over your form input element to create the desired array.
Do it like below:
It will provide output like this:
Working snippet:
IDs need to be unique, so for now we cannot us that.
If we instead loop over the engines it will work
No need for jQuery.