Tôi đã gặp phải một chút vấn đề với việc truyền tham số bài viết tới một hành động điều khiển thông qua một cuộc gọi ajax. Tôi không chắc tại sao nó lại làm như vậy, bởi vì các cuộc gọi ajax khác thực hiện như dự định. Câu trả lời tôi nhận được là như sau:thông số bài viết không được thông qua khuôn khổ zend 2
Notice: Undefined index: user_bio in C:\xampp\htdocs\module\Members\src\Members\Controller\ProfileController.php on line 149
Mã điều khiển là:
public function changebioAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
if ($this->request->isPost()) {
try {
$params = $this->params()->fromPost();
$this->getProfileService()->editProfile(array('bio' => ltrim($params['user_bio'])));
} catch (ProfileException $e) {
$this->flashMessenger()->addErrorMessage($e->getMessage());
return $this->redirect()->toRoute('members/profile', array('action' => 'change-failure'));
}
}
return $view_model;
}
Các javascript và html:
// handle quick edit of bio
function quickEditBio(element, button) {
var edited_data = $(element).html();
var data = $(element).blur(function() {
edited_data = $(element).map(function() {
return this.innnerHTML;
}).get();
});
$(button).on('click', function() {
$.ajax({
method : "POST",
url : "/members/profile/change-bio",
data : {
user_bio : edited_data[0]
}
}).done(function(msg) {
// bio saved
// go back to profile page
//location.href = '/members/profile';
alert(msg);
}).fail(function() {
alert("Error changing bio, please try again.");
});
});
}
<button onclick="expand('bio')" class="w3-btn-block w3-theme-d2 w3-left-align">
<i class="fa fa-book fa-fw w3-margin-right"></i> Bio
</button>
<div id="bio" class="w3-accordion-content w3-container">
<div class="w3-display-container">
<p id="bio-user" contenteditable="true">
<?php echo $this->layout()->bio; ?>
</p>
<div class="w3-display-right">
<button class="w3-btn-block w3-theme-d2" id="save-bio">Save</button>
</div>
<script type="text/javascript">
quickEditBio('#bio-user[contenteditable=true]', $('#save-bio'));
</script>
</div>
</div>
Như đã trình bày trước đây, lỗi tôi nhận được là chỉ mục user_bio không được xác định:
Notice: Undefined index: user_bio in C:\xampp\htdocs\module\Members\src\Members\Controller\ProfileController.php on line 149
Phần thực sự khó hiểu là các phần khác sử dụng cùng mã này và hoạt động tốt.
Mọi trợ giúp sẽ được đánh giá cao.
Cảm ơn!
cảm ơn, đã hoạt động hoàn hảo. – user2101411