【发布时间】:2016-04-06 16:32:30
【问题描述】:
当按下 Delete 按钮时,我正在尝试使用 AJAX/jQuery 删除我的 MYSQL 数据库中的一行。我正在使用 Zend Framework 1.12。
直到 alert(del_id) 部分我的代码是 正常工作:当我按下删除按钮时,会显示一个具有匹配 ID 的窗口。
但是在页面和我的数据库中删除行的部分不起作用。谁能告诉我这个问题?
我也对使用 $.ajax() 和 Zend 有一些疑问:是否正确地将删除操作的视图脚本的路径放在 url 中?这样做会在我的控制器上调用我的 deleteAction() 函数吗?
这是带有 java 脚本的布局:
<!-- application/views/scripts/users-data/adminpage.phtml -->
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(
function(){
$(".delete_class").click(
function(){
var del_id = $(this).attr('id');
var rowElement = $(this).parent().parent();
alert(del_id);
$.ajax({type:'POST',
url:'/users-data/delete.phtml',
data: {'delete_id':del_id},
success:function(data) {
if(data=="YES") {
rowElement.fadeOut().remove();
alert("success");
}else { alert("erro");
}}
});
});
});
</script>
</head>
<body>
<center>
<?php
echo "Users List";
?>
<table border="1">
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<?php foreach($this->entries as $entry) : ?>
<tr>
<td><?php echo $entry->name;?></td>
<td><?php echo $entry->email;?></td>
<td><?php echo $entry->id;?></td>
<td> <button id="<?php echo $entry->id; ?>" class="delete_class"> Delete</button></td>
</tr>
<?php endforeach; ?>
</table>
这是控制器中的代码:
我测试并工作的 deleteUser() 函数。
public function deleteAction()
{
//$id = $_POST['delete_id'];
$request = $this->getRequest();
$id= $request->getPost('delete_id');
$mapper = new Application_Model_UsersDataMapper();
$mapper->deleteUser($id);
}
【问题讨论】:
标签: php jquery mysql ajax zend-framework