删除MySQL表行与jQuery阿贾克斯 [英] Deleting mySQL Table Row with jQuery Ajax

查看:140
本文介绍了删除MySQL表行与jQuery阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使它所以当我点击一个跨度图标会,我使用jQuery的Ajax发送ID发送的article_id到我的PHP SQL网页,其中删除我的文章中,ID发送好吗jQuery开发边而过的HTTP POST请求做我的表行还是有的,任何人都可以看到,如果有些事情错了我的code,先谢谢了!

 < PHP
        $ sql_categories =SELECT art_id,art_title,art_company,art_cat_id,art_sta_id,art_date,art_rating,art_price,cat_id,cat_name,sta_id,sta_name
                从app_articles LEFT JOIN app_categories
                ON app_articles.art_cat_id = app_categories.cat_id
                LEFT JOIN app_status
                ON app_articles.art_sta_id = app_status.sta_id
                ORDER BY art_order ASC;

            如果($结果=查询($ sql_categories)){
                $名单=阵列();

                而($数据= mysqli_fetch_assoc($结果)){
                    array_push($列表中,$数据);
                }

                的foreach($清单,$ I => $行){
                ?>
                    < D​​IV CLASS =行ID =PAGE_<?PHP的回声$行['art_id'];?>>
                        < D​​IV CLASS =列中的一个>
                            <跨度类=图标,小动>< / SPAN>
                            < A HREF =编辑 -  article.php art_id =<?PHP的echo $行['art_id'];?>><跨度类=图标,小编辑>< / SPAN&GT ;&所述; / a取代;
                            <跨度ID =<?PHP的回声$行['art_id'];?>中类=图标,小杂鱼>< / SPAN>
                        < / DIV>
                        < D​​IV CLASS =两栏>< P>< PHP的echo $行['art_title']; ?>< / P>< / DIV>
                        < D​​IV CLASS =两栏>< P>< PHP的echo $行['art_company']; ?>< / P>< / DIV>
                        < D​​IV CLASS =两栏>< P>< PHP的echo $行['cat_name']; ?>< / P>< / DIV>
                        < D​​IV CLASS =列中的一个>< P>< PHP的echo $行['art_date']; ?>< / P>< / DIV>
                        < D​​IV CLASS =列中的一个>< P>< PHP的echo $行['art_rating']; ?>< / P>< / DIV>
                        < D​​IV CLASS =列中的一个>< P>< PHP的echo $行['art_price']; ?>< / P>< / DIV>
                        < D​​IV CLASS =列中的一个>< P>< PHP的echo $行['sta_name']; ?>< / P>< / DIV>
                        < D​​IV CLASS =列中的一个>< A HREF =><跨度类=图标,小星>< / SPAN>< / A>< / DIV>
                    < / DIV>
                < PHP
                }
            }
            其他 {
                回声不及格;
            }
        ?>


jQuery的

        $(文件)。就绪(函数(){

                $(垃圾)。点击(函数(){

            VAR del_id = $(本).attr('身份证');

            $阿贾克斯({
                键入:POST,
                网址:'AJAX-delete.php',
                数据:'delete_id ='+ del_id,
                成功:功能(数据){
                    如果(数据){

                    }
                    其他 {

                    }
                }

            });
        });

    });


PHP MySQL的声明

    如果(使用isset($ _ POST ['delete_id'])){

        $ sql_articles =DELETE FROM app_articles WHERE art_id =$ _ POST ['delete_id']。

        如果(查询($ sql_articles)){
            回声YES;
        }
        其他 {
            回声否;
        }
    }
其他 {
    回声不及格;
}


?>
 

解决方案

你行还是存在的,因为AJAX调用不会刷新页面的原因。如果你希望你的行中删除你必须做的是这样的:

假设你的跨度click事件是行内

  $(垃圾)。点击(函数(){
   VAR del_id = $(本).attr('身份证');
   。VAR rowElement = $(本).parent()父(); //抢行

   $阿贾克斯({
            键入:POST,
            网址:'AJAX-delete.php',
            数据:{delete_id:del_id},
            成功:功能(数据){
                如果(数据==是){
                   rowElement.fadeOut(500)上卸下摆臂();
                }
                其他 {

                }
            }
    });
 

替换:

 数据:'delete_id ='+ del_id,
 

 数据:delete_id:del_id,
 

I'm trying to make it so when I click a span icon it will send the article_id to my php sql page which deletes my article, I'm using jQuery Ajax to send the id, the id sends alright on the jQuery side but after the http post request is done my table row is still there, can anyone see if somethings wrong with my code, thanks in advance!

    <?php
        $sql_categories = "SELECT art_id, art_title, art_company, art_cat_id, art_sta_id, art_date, art_rating, art_price, cat_id, cat_name, sta_id, sta_name
                FROM app_articles LEFT JOIN app_categories
                ON app_articles.art_cat_id = app_categories.cat_id
                LEFT JOIN app_status
                ON app_articles.art_sta_id = app_status.sta_id
                ORDER BY art_order ASC"; 

            if($result = query($sql_categories)){
                $list = array();

                while($data = mysqli_fetch_assoc($result)){
                    array_push($list, $data);
                }

                foreach($list as $i => $row){ 
                ?>
                    <div class="row" id="page_<?php echo $row['art_id']; ?>">
                        <div class="column one">
                            <span class="icon-small move"></span>
                            <a href="edit-article.php?art_id=<?php echo $row['art_id']; ?>"><span class="icon-small edit"></span></a>
                            <span id="<?php echo $row['art_id']; ?>" class="icon-small trash"></span>
                        </div>
                        <div class="column two"><p><?php echo $row['art_title']; ?></p></div>
                        <div class="column two"><p><?php echo $row['art_company']; ?></p></div>
                        <div class="column two"><p><?php echo $row['cat_name']; ?></p></div>
                        <div class="column one"><p><?php echo $row['art_date']; ?></p></div>
                        <div class="column one"><p><?php echo $row['art_rating']; ?></p></div>
                        <div class="column one"><p><?php echo $row['art_price']; ?></p></div>
                        <div class="column one"><p><?php echo $row['sta_name']; ?></p></div>
                        <div class="column one"><a href=""><span class="icon-small star"></span></a></div>
                    </div>
                <?php
                }
            }
            else {
                echo "FAIL";
            }
        ?>


jQuery

        $(document).ready(function(){

                $(".trash").click(function(){

            var del_id = $(this).attr('id');

            $.ajax({
                type:'POST',
                url:'ajax-delete.php',
                data: 'delete_id='+del_id,
                success:function(data) {
                    if(data) {

                    } 
                    else {

                    }   
                }

            }); 
        });

    });


PHP mySQL Statement

    if(isset($_POST['delete_id'])) {

        $sql_articles = "DELETE FROM app_articles WHERE art_id = ".$_POST['delete_id'];

        if(query($sql_articles)) {
            echo "YES";
        }
        else {
            echo "NO";
        }
    }
else {
    echo "FAIL";
}


?>

解决方案

the reason your row is still there because AJAX call does not refresh the page. If you want your row to be removed you gotta do something like:

ASSUMING that your span click event is inside the row

 $(".trash").click(function(){
   var del_id = $(this).attr('id');
   var rowElement = $(this).parent().parent(); //grab the row

   $.ajax({
            type:'POST',
            url:'ajax-delete.php',
            data: {delete_id : del_id},
            success:function(data) {
                if(data == "YES") {
                   rowElement.fadeOut(500).remove();
                } 
                else {

                }   
            }
    });

Replace:

            data: 'delete_id='+del_id,

with:

            data: delete_id : del_id,

这篇关于删除MySQL表行与jQuery阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆