微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 当我单击删除按钮时查询未运行,即使.success说它有效

当我点击切换下来菜单中的删除时,我创建了一个模型.然后,它会通过单击旁边的按钮提供我可以删除的数据列表.但由于某种原因,我无法弄清楚页面重新加载什么都没有被删除.我在javascript成功后添加一个警报’success’.调用此警报但不删除数据.引导程序和jQuery链接位于索引页面中,这些链接都完美无缺.这个问题已经傻眼了,如果有人能说清楚它,我会非常感激.

这是html:

<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>

这是类的PHP代码.评论全部来自数据库完美,看起来很完美.问题是删除它们.

<?PHP

class Comments{

    public static function delete($cl_id)
{


    $db = new PDO('MysqL:host=localhost;dbname=comment;charset=utf8', 'root', '');
    $stmt = $db->prepare("DELETE FROM `comments` WHERE id = :cl_id");

    $stmt->bindParam(':cl_id', $cl_id);
    if ($stmt->execute()) {
        return true;
    } else {
        return null;
    }
}

}

和javascript:

$(document).ready(function(){
add_delete_handlers();
});

function add_delete_handlers(){
$('.comment_delete_button').each(function(){
    var btn = this;
    $(btn).click(function(){

        comment_delete(btn.id);

    });
});
}

function comment_delete(_comment_id){

$.post('comment_area_ajax/comment_delete.PHP', {
        task: 'comment_delete',
        comment_id: _comment_id
    }
).success(function(data){
        alert('Success');
        $('#_' + _comment_id).detach();
    });

 }

最后是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';
}

?>

解决方法:

尝试:

$(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();
    });
}

试试你的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));
        }
    }
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐