Javascript 当我单击“删除”按钮时,查询未运行,即使如此。成功表明它有效
我制作了一个模型,当我从切换菜单中单击“删除”时,该模型被调用。然后,它给出了一个数据列表,我可以通过单击旁边的按钮来删除这些数据。但由于某些原因,我无法确定页面何时重新加载,没有任何内容被删除。在javascript中成功之后,我添加了一个警报“success”。已调用此警报,但未删除数据。引导和jQuery链接都在索引页中,它们都可以完美地工作。这个问题让人目瞪口呆,如果有人能解释一下,我将不胜感激 这是html:Javascript 当我单击“删除”按钮时,查询未运行,即使如此。成功表明它有效,javascript,php,jquery,sql,ajax,Javascript,Php,Jquery,Sql,Ajax,我制作了一个模型,当我从切换菜单中单击“删除”时,该模型被调用。然后,它给出了一个数据列表,我可以通过单击旁边的按钮来删除这些数据。但由于某些原因,我无法确定页面何时重新加载,没有任何内容被删除。在javascript中成功之后,我添加了一个警报“success”。已调用此警报,但未删除数据。引导和jQuery链接都在索引页中,它们都可以完美地工作。这个问题让人目瞪口呆,如果有人能解释一下,我将不胜感激 这是html: <div class="modal fade" id="delete_
<div class="modal fade" id="delete_comment" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="form-horizontal" role="form">
<div class="modal-header">
<h4>Delete comments</h4>
</div>
<div class="modal-body">
<?php $comment_array = comments::get_comments($username); ?>
<?php if(isset($comment_array) && is_array($comment_array)): ?>
<?php
foreach ($comment_array as $key => $comment):
foreach($comment as $key2 => $comment2):
$max = count($comment_array);
$i=1;
while($i<=$max):
?>
<?php
$comment_id = $comment2->id;
$comment_name = $comment2->comment_name;
$comment_type = $comment2->comment_type;
?>
<form class="comment_delete_area_wrapper" action="" method="post">
<div class="comment_delete_wrapper">
<ul>
<li class="comment_delete_comments">
<?php echo $comment_type ?> : <?php echo $comment_name; ?>
</li>
</ul>
<div class="comment_delete_button_wrapper">
<input type="submit" id="<?php echo $comment_id; ?>"
class="btn btn-danger comment_delete_button" value="Delete"/>
</div>
<br>
<hr>
<input type="hidden" value="<?php echo $comment_id; ?>"
id="comment_delete_id" name="comment_delete_name"/>
</div>
</form>
<?php $i++; ?>
<?php endwhile;?>
<?php endforeach; ?>
<?php endforeach;?>
<?php endif; ?>
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
</div>
</div>
</div>
</div>
</div>
最后是AJAX调用的代码:
<?php
if(isset($_POST['task']) && $_POST['task'] =='comment_delete') {
require_once '../comment_area_sql/models/comment_delete_model.php';
require_once '../comment_area_sql/models/comments.php';
if(class_exists('Comments')){
if(Comments::delete($_POST['comment_id'])){
echo 'true';
}else{
echo 'false';
}
?>
试试:
对于ajaxed文件,请尝试以下操作:
<?php
if(isset($_POST['task']) && $_POST['task'] =='comment_delete') {
require_once '../comment_area_sql/models/comment_delete_model.php';
require_once '../comment_area_sql/models/comments.php';
if(class_exists('Comments')){
if(Comments::delete($_POST['comment_id'])){//note the change
$message = 'comment deleted';
}else{
$message = 'we have a error';
}
return json_encode(array('message'=>$message));
}
}
?>
在上一个文件中,您已经定义了函数,但没有调用它。您应该有类似于delete($\u POST['comment\u id')
在中添加了函数,但仍然存在相同的问题。它应该是delete($\u POST['comment\u id'])
由于您在ajax调用中传入了comment\u id:\u comment\u id
,所以您说的对,我没有调用该函数。事实上,我不需要它,所以我删除了它。然后只需将其更改为捕获post值“comment_id”并完成。谢谢,我试过这个。同样的事情。仍然没有删除。您的控制台中是否有错误?
<?php
if(isset($_POST['task']) && $_POST['task'] =='comment_delete') {
require_once '../comment_area_sql/models/comment_delete_model.php';
require_once '../comment_area_sql/models/comments.php';
if(class_exists('Comments')){
if(Comments::delete($_POST['comment_id'])){
echo 'true';
}else{
echo 'false';
}
?>
$(document).ready(function() {
add_delete_handlers();
});
function add_delete_handlers() {
$('.comment_delete_button').click(function() {
comment_delete($('this').attr('id'));
});
}
function comment_delete(_comment_id) {
$.post('comment_area_ajax/comment_delete.php', {
task: 'comment_delete',
comment_id: _comment_id
}, function(data) {
alert('Success');
$('#_' + _comment_id).detach();
});
}
<?php
if(isset($_POST['task']) && $_POST['task'] =='comment_delete') {
require_once '../comment_area_sql/models/comment_delete_model.php';
require_once '../comment_area_sql/models/comments.php';
if(class_exists('Comments')){
if(Comments::delete($_POST['comment_id'])){//note the change
$message = 'comment deleted';
}else{
$message = 'we have a error';
}
return json_encode(array('message'=>$message));
}
}
?>