I am trying to save some data to a database via PHP and jQuery but after the 3rd index, the 3rd index is used instead of the 4th index and 5th index is not used entirely. I've tested the data being sent via jQuery and it all lines up perfectly but the PHP part is repeating the 3rd index value twice.
Here is my code:
jQuery/JavaScript:
// get the creating profile data function getProfileData(element, button) { var profile_data = $(element).html();
var data = $(element).blur(function() {
profile_data = $(element).map(function() {
return this.innerHTML;
}).get();
});
$(button).on('click', function() {
var edited_content = profile_data.join(", ");
var split = edited_content.split(", ");
$.ajax({
method : "POST",
url : "/members/profile/create-profile",
data : {
display_name : split[0],
email_address : split[1],
age : split[2],
location : split[3],
bio : split[4]
},
}).done(function() {
alert("Profile saved!")
}).fail(function() {
alert("Error saving profile, please try again");
});
});
}
and here is the PHP part (using zend framework 2)
public function createprofileAction()
{
if ($this->getProfileService()->checkIfProfileSet()) {
return $this->redirect()->toRoute('members/profile', array('action' => 'index'));
}
if ($this->request->isPost()) {
$params = $this->params()->fromPost();
$this->getProfileService()->makeProfile(array(
'display_name' => $params['display_name'],
'email_address' => $params['email_address'],
'age' => $params['age'],
'location' => $params['location'],
'bio' => $params['bio'],
));
}
}
html:
<div class="w3-card-2 w3-round">
<div class="w3-accordion w3-white">
<button onclick="expand('side-1')"
class="w3-btn-block w3-theme-l1 w3-left-align">
<i class="fa fa-edit fa-fw w3-margin-right"></i> Display Name
</button>
<div id="side-1" class="w3-accordion-content w3-container">
<p contenteditable="true">Click to set your display name</p>
</div>
<button onclick="expand('side-2')"
class="w3-btn-block w3-theme-l1 w3-left-align">
<i class="fa fa-edit fa-fw w3-margin-right"></i> Email Address
</button>
<div id="side-2" class="w3-accordion-content w3-container">
<p contenteditable="true">Click to set your email address</p>
</div>
<button onclick="expand('side-3')"
class="w3-btn-block w3-theme-l1 w3-left-align">
<i class="fa fa-edit fa-fw w3-margin-right"></i> My Age
</button>
<div id="side-3" class="w3-accordion-content w3-container">
<p contenteditable="true">Click to set your age</p>
</div>
<button onclick="expand('side-4')"
class="w3-btn-block w3-theme-l1 w3-left-align">
<i class="fa fa-edit fa-fw w3-margin-right"></i> My Location
</button>
<div id="side-4" class="w3-accordion-content w3-container">
<p contenteditable="true">Click to set your location</p>
</div>
<button onclick="expand('side-5')"
class="w3-btn-block w3-theme-l1 w3-left-align">
<i class="fa fa-edit fa-fw w3-margin-right"></i> Bio
</button>
<div id="side-5" class="w3-accordion-content w3-container">
<p contenteditable="true">Click to set your Bio</p>
</div>
</div>
</div>
<br><br>
<div class="w3-display-container">
<div class="w3-display-bottomright">
<button class="w3-btn-block w3-theme-l1" id="save">Save</button>
</div>
</div>
<script type="text/javascript">
getProfileData('[contenteditable]', $('#save'));
</script>
To better explain it, age is inserted but then it fills in the location field in the database table and bio takes the location value.
I can provide more information if need be.
Appreciate any help!
Thanks