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

在XDK中使用Ajax和php删除sql中的数据

我想在xdk中按下删除按钮时删除sql中的一行数据.我搜索了一些代码,但仍然没有删除数据.

这是PHP文件(delete.PHP)

<?PHP
include('dbcon.PHP');

$foodid = $_POST['foodid'];

$query = "DELETE FROM menu WHERE id ='$foodid'";
$result=MysqL_query($query);

if(isset($result)) {
echo "YES";
} else {
echo "NO";
}
?>

在这是我的ajax代码.

$("#btn_delete").click( function(){
alert("1");

    var del_id = $(this).attr('foodid');
    var $ele = $(this).parent().parent();

alert("2");

    $.ajax({
        type: 'POST',
        url: 'http://localhost/PHP/delete.PHP',
        data: { 'del_id':del_id },
        dataType: 'json',
        succes: function(data){
alert("3");
            if(data=="YES"){
                $ele.fadeOut().remove();
            } else {
                alert("Cant delete row");
            }
        }
    });
});

如您所见,当我在xdk中运行程序时,我放置了警报以了解我的代码是否正在处理.它最多只警报到alert(“ 2”); .而不是继续到3.所以我认为我的Ajax在这里错误的部分.我对ajax有点陌生.

解决方法:

                            <?PHP
                                $sqli= "*select * from temp_salesorder *";
                                $executequery= MysqLi_query($db,$sqli);
                                while($row = MysqLi_fetch_array($executequery,MysqLI_ASSOC))
                                {       
                            ?>                  

 //"class= delbutton" is use to delete data through ajax

<a href="#" **id =<?PHP echo $row['transaction_id']; ?>** class="**delbutton**" title="Click To Delete"><button> Cancel</button></a>





     <!-- language: lang-js -->

     //Ajax Code
        <script type="text/javascript">
        $(function() {


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

        //Save the link in a variable called element
        var element = $(this);

        //Find the id of the link that was clicked
        var del_id = element.attr("id");

        //Built a url to send
        var info = 'id=' + del_id;

         $.ajax({
           type: "GET",
           url: "deletesales.PHP",
           data: info,
           success: function(){

           }
         });
                 $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
                .animate({ opacity: "hide" }, "slow");



        return false;

        });

        });
        </script>

//deletesales.PHP

<?PHP
$db_host        = 'localhost';
$db_user        = 'root';
$db_pass        = '';
$db_database    = 'pos'; 
$db = MysqLi_connect($db_host,$db_user,$db_pass,$db_database);
    $id=$_GET['id'];  <!-- This id is get from delete button -->


    $result = "DELETE FROM temp_salesorder WHERE transaction_id= '$id'";
     MysqLi_query($db,$result);

?>



    <!-- end snippet -->

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

相关推荐