我试图通过PHP和jQuery将一些数据保存到数据库但是在第3个索引之后,使用第3个索引而不是第4个索引,并且不完全使用第5个索引。我已经测试了通过jQuery发送的数据,它们都完美排列,但PHP部分重复了第三个索引值两次。
这是我的代码:
的jQuery / JavaScript的:
//获取创建配置文件数据 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");
});
});
}
这里是PHP部分(使用zend框架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>
为了更好地解释它,插入了age,但是它填充了数据库表中的location字段,bio获取了位置值。
如果需要,我可以提供更多信息。
感谢任何帮助!
由于