I have a 7 steps form and this is the last step which shows all the info the user typed in former pages. All the data is dynamically appended to each <td class="table_info_ans">
using jQuery.
I need to wrap up these data into json format like below and send it to backend:
{
"personal": {
"gender": "F",
"firstName": "Heeeeey",
"lastName": "Beauty",
"localName": "Girl",
"birthday": "1992/10/28"
},
"interests": {
"destinations": [
"UK",
"USA",
"CA"
],
}
}
Any method to do this?? The table structure looks like:
<form action='/student-registration/process'>
<hr>
<div class="blue_font_title">ABOUT YOU
<div class="link_page link_page_1" data-hook="form_step1"><span class="page_back_arrow"></span></div>
</div>
<table class="info_table" id="personal">
<tbody>
<tr>
<td class="table_info_title">Gender:</td>
<td class="table_info_ans student_gender" name="gender"></td>
</tr>
<tr>
<td class="table_info_title">Local name:</td>
<td class="table_info_ans student_local_name" name="localName"></td>
</tr>
<tr>
<td class="table_info_title">First name:</td>
<td class="table_info_ans student_eng_firstname" name="firstName"></td>
</tr>
<tr>
<td class="table_info_title">Last name:</td>
<td class="table_info_ans student_eng_lastname" name="lastName"></td>
</tr>
<tr>
<td class="table_info_title">Birthday:</td>
<td class="table_info_ans student_birthday" name="birthday"></td>
</tr>
</tbody>
</table>
<hr>
<div class="blue_font_title">COUNTRY AND TIME YOU WANT TO GO
<div class="link_page link_page_2" data-hook="form_step2"><span class="page_back_arrow"></span></div>
</div>
<table class="info_table" id="interest">
<tbody>
<tr>
<td class="table_info_title">Destination:</td>
<td class="table_info_ans student_study_destination"></td>
</tr>
...
</tbody>
</table>
<button type="submit" id="submit_button" class="submit_button">SUBMIT YOUR PROFILE</button>
Now, I'm trying to wrap up these data into the format I want manually....
var personal_title_arr = [];
var personal_ans_arr = [];
var obj_per = {};
$('#personal tr').each(function() {
var personal_title = $(this).find('.table_info_ans').attr('name');
var personal_ans = $(this).find('.table_info_ans').text();
personal_title_arr.push(personal_title);
personal_ans_arr.push(personal_ans);
});
for (var i = 0; i < personal_title_arr.length; i++) {
obj_per[personal_title_arr[i]] = personal_ans_arr[i];
}
var asJSON_per = JSON.stringify(obj_per);
Because there are four tables in this form, so I have to wrap them up manually four times. I think the way I'm doing is really ridiculous..... Are there any tips that I can for loop just ONE time?