I have two pieces of content: one for employment history, and one for address history.
Given that a person can have multiple forms for employment history (if they have been at more than 1 employer) or address history, I have the option there to click a plus symbol to add a form.
Thus, I have an .click() event handler that when clicked appends a new form.
I have structured the data like this in JS and POSTed it to the server, except I'm having trouble doing this. I must insert a record for each index in employment or address.
var contents = {
employment: {
df1e: [
],
dt1e: [
],
c1e: [
],
p1e: [
],
l1e: [
]
},
address: {
df1a: [
],
dt1a: [
],
sa1a: [
],
c1a: [
],
st1a: [
],
zc1a: [
],
},
name: $('#first_name').val() + ' ' + $('#last_name').val()
};
I then wrote $.each() functions for each element and added them to the contents array. Ex (except for each damn one):
$('.df1e').each(function(i, obj) {
contents.employment.df1e[i] = this.value;
});
There must be a better way to do this. How can I do this without too much trouble?
<?php
$data = json_decode(file_get_contents('php://input'), true);
if($data) {
foreach($data['employment'] as $employment) {
$bean->name = $data['name'] . ' - Employment';
$bean->type_c = 'Employment';
$bean->date_from_c = $employment['df1e'];
$bean->date_to_c = $employment['dt1e'];
$bean->company_c = $employment['c1e'];
$bean->position_c = $employment['p1e'];
$bean->location_c = $employment['l1e'];
$bean->save();
}
foreach($data['address'] as $address) {
$bean->name = $data['name'] . ' - Address';
$bean->type_c = 'Address';
$bean->date_from_c = $address['df1a'];
$bean->date_to_c = $address['dt1a'];
$bean->street_address_city_c = $address['c1a'];
$bean->street_address_state_c = $address['st1a'];
$bean->street_address_postalcode_c = $address['zc1a'];
$bean->street_address_c = $address['sa1a'];
$bean->save();
}
}
echo json_encode(array('response' => $data));
Ignore the bean stuff -- it's SugarCRM's API. It's the same thing as inserting into MySQL.
typecolumn in the DB that should be equal to whatever the type of history record is.