I have a section where users can enter their educational history. each time the "Add education" anchor tag is clicked and set of three inputs is created. the education_institution, education_outcome and education_date. So each user can have one or one hundred.
This is then saved in MYSQL as JSON data. My question is how can I then get it from the database and loop through (either with PHP or javascript) the JSON and display each set in 3 inputs row as seen below in the HTML?
Any help is most appreciated.
HTML:
<p class="mt-3 text-left"> Education </p>
<div class="row block">
<div class="col-md-3">
<input type="text" name="education_institution[]" placeholder="Institution" value="<?php echo($decoded_data->education->education_institution); ?>">
</div>
<div class="col-md-3">
<input type="text" name="education_outcome[]" placeholder="Outcome" value="<?php echo($decoded_data->education->education_outcome); ?>">
</div>
<div class="col-md-3">
<input type="Date" name="education_date[]" placeholder="Date of completion" value="<?php echo($decoded_data->education->education_date); ?>">
</div>
<div class="col-md-3 text-center">
<button href="#" class="able_btn m-0 w-100"> remove </button>
</div>
</div>
<div class="text-center mt-3">
<a class="add_education" href=""> Add education </a>
</div>
JQUERY:
$('.add_education').click(function(evt) {
evt.preventDefault();
$('.block:last').after('<div class="row block"><div class="col-md-3"><input type="text" name="education_institution[]" placeholder="Institution"></div><div class="col-md-3"><input type="text" name="education_outcome[]" placeholder="Outcome"></div><div class="col-md-3"><input type="Date" name="education_date[]" placeholder="Date of completion"></div><div class="col-md-3 text-center"><button class="able_btn m-0 w-100 remove"> remove </button></div></div>');
});
PHP:
$json_data = array(
'profession' => $profession,
'personal_statement' => $personal_statement,
'education' => [
'education_institution' => $education_institution,
'education_outcome' => $education_outcome,
'education_date' => $education_date],
'employment' => [
'job_title' => $job_title,
'job_location' => $job_location,
'job_duration' => $job_duration]);
$data = json_encode($json_data);
JSON:
{
"profession": "",
"personal_statement": "",
"education": {
"education_institution": [
"school one",
"school two"
],
"education_outcome": [
"out one",
"out two"
],
"education_date": [
"2017-11-01",
"2017-11-04"
]
},
"employment": {
"job_title": [
""
],
"job_location": [
""
],
"job_duration": [
""
]
}
}
foreach ($data as $row)andJSON.parse(json).forEach(...)