这是我目前使用的代码,无法理解如何插入ajax代码
我在这个网站上已经阅读了更多帖子,但我仍然无法理解我是如何插入ajax的代码
<?php
class FacController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function deleteAction()
{
if ($this->getRequest()->isPost()) {
$del = $this->getRequest()->getPost('del');
if ($del == 'Yes') {
$id = $this->getRequest()->getPost('id');
$details = new Application_Model_DbTable_Details();
$details->deleteDetails($id);
}
$this->_helper->redirector('index');
} else {
$id = $this->_getParam('id', 0);
$details = new Application_Model_DbTable_Details();
$this->view->details = $details->getDetails($id);
}
}
}
答案 0 :(得分:1)
让我们假设您的索引操作显示了您要删除的内容的列表。
通过ajax删除的方式非常简单。
现在看起来如何:
此视图应包含类似jQuery的以下代码:
$("#button").click(function(){
var menuId = $("ul.nav").first().attr("id");
var request = $.ajax({
url: "/index/delete",
type: "POST",
data: {id : menuId},
dataType: "html"
});
request.done(function(msg) {
$("#log").html( msg );
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
});
public function deleteAction()
{
$this->_helper->layout->disableLayout(); //This disables the layout
$this->_helper->viewRenderer->setNoRender(TRUE); //This disables need for delete.phtml
if ($this->getRequest()->isPost()) {
$id = $this->getRequest()->getPost('id');
$details = new Application_Model_DbTable_Details();
$status=$details->deleteDetails($id); //Lets add a status check to know if we have successfully deleted something
if($status){
echo json_encode(true);
}
else{
echo json_encode(false);
}
}
即使我没有对此代码进行过广泛测试,但这应该会让您走上正轨。