I have a form for setting up store timings as follows:
<table class="table">
<tbody>
<tr>
<td tabindex="0">Monday</td>
<td><label class="Storeopen">Open</label></td>
<td><input type="time" id="day_1_open" name="day_1[]" value="16:00"></td>
<td><label class="Storeclose">Close</label> </td>
<td><input type="time" id="day_1_close" name="close" value="00:00"></td>
</tr>
<tr>
<td tabindex="0">Tuesday</td>
<td><label class="Storeopen">Open</label></td>
<td><input type="time" id="day_2_open" name="open" value="16:00"></td>
<td><label class="Storeclose">Close</label> </td>
<td><input type="time" id="day_2_close" name="close" value="00:00"></td>
</tr>
<tr>
<td tabindex="0">Wednesday</td>
<td><label class="Storeopen">Open</label></td>
<td><input type="time" id="day_3_open" name="open" value="16:00"> </td>
<td><label class="Storeclose">Close</label> </td>
<td><input type="time" id="day_3_close" name="close" value="00:00"></td>
</tr>
<tr>
<td tabindex="0">Thursday</td>
<td><label class="Storeopen">Open</label></td>
<td><input type="time" id="day_4_open" name="open" value="16:00"> </td>
<td><label class="Storeclose">Close</label> </td>
<td><input type="time" id="day_4_close" name="close" value="00:00"></td>
</tr>
<tr>
<td tabindex="0">Friday</td>
<td><label class="Storeopen">Open</label></td>
<td><input type="time" id="day_5_open" name="open" value="16:00"> </td>
<td><label class="Storeclose">Close</label> </td>
<td><input type="time" id="day_5_close" name="close" value="00:00"></td>
</tr>
<tr>
<td tabindex="0">Saturday</td>
<td><label class="Storeopen">Open</label></td>
<td><input type="time" id="day_6_open" name="open" value="16:00"> </td>
<td><label class="Storeclose">Close</label> </td>
<td><input type="time" id="day_6_close" name="close" value="00:00"></td>
</tr>
<tr>
<td tabindex="0">Sunday</td>
<td><label class="Storeopen">Open</label></td>
<td><input type="time" id="day_7_open" name="open" value="16:00"> </td>
<td><label class="Storeclose">Close</label> </td>
<td><input type="time" id="day_7_close" name="close" value="00:00"></td>
</tr>
</tbody>
</table>
Now. To get data, I am using the following jQuery code:
var newTiming = new Array();
for(var n=1; n<=7; n++){
newTiming["day_"+[n]] = {"open": $("#day_"+n+"_open").val(), "close":$("#day_"+n+"_close").val()};
console.log(newTiming);
}
My problem is I can get the data in the console but when I try to store the array into a hidden text field to pass it to my PHP, I am not getting anywhere.
I tried to put the array in the hidden field by the following code:
$("#deliveryTimings").val(JSON.stringify(newTiming));
What I need to get is an array in the following form:
"day_1" => array(
"open" => "16:00",
"close" => "00:00"
),
This is part of a larger form. I am trying to save each section by jQuery. There is no tag and lots of other input fields on the page. My save button for each section addresses that particular section. Thats why I cant use serializeArray()? and am using this jQuery code to get the data.
I am hoping to get help from community here. Thank you.
Update: I found the ultimate solution by using jQuery Serialize Object by Paul Maček on https://github.com/macek/jquery-serialize-object