用jQuery执行mysql_query

时间:2014-07-08 12:29:17

标签: javascript php jquery mysql ajax

我有一个装有产品的购物车。我有每个产品的删除按钮。我想要的是执行mysql_query:

mysql_query("DELETE FROM bim_cart WHERE coupon_id = '{$_GET['delete']}' AND session_code = '{$_SESSION['code']}'");

按钮代码:

<div class="remove">
<a href="<?php echo $address . '/index.php?lang=lv&page=cart&delete=' . $item['id'];?>" class="color2">
    <i class="fa fa-trash-o fa-fw color2"></i>
</a>

可以不重新加载页面,但刷新信息吗?

3 个答案:

答案 0 :(得分:0)

你必须将参数传递给你的函数

<a onClick="deleteProduct(<?echo $id_to_delete;?>)"...></a>

在剧本中

<script type="text/javascript">
function deleteProduct(id){
    $.post("delete.php", {id: id});
return false;
}
</script>

在删除页面中编写查询

mysql_query("DELETE FROM bim_cart WHERE coupon_id = '{$_GET['delete']}' AND session_code = '{$_SESSION['code']}'");

答案 1 :(得分:0)

使用AJAX请求可以解决这个问题。试试这样的事情

在HTML页面

<div class="remove" id="id-<?=$item['id']?>">
<a href="javascript:void(0);" data-id="<?=$item['id']?>" class="color2">
    <i class="fa fa-trash-o fa-fw color2"></i>
</a>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('.color2').live('click', function(){
        var id = $(this).attr('data-id');
            $.ajax({
            url: 'delete.php'
            ,data:{delete: id}
            ,type: 'GET'
            ,success:function(data){
                if(data == 'success')
                {
                    //clear your deleted product div/html element something like below
                    $('#id-' + id).remove();
                }
            }
        });
    });
});
</script>

添加一个PHP文件delete.php&amp;执行删除操作

if(isset($_GET['delete']))
{
    $delete_id = $_GET['delete'];
    $deleted = mysql_query("DELETE FROM bim_cart WHERE coupon_id = '$delete_id' AND session_code = '{$_SESSION['code']}'");
    if($deleted){
       echo 'success';
    }
}

答案 2 :(得分:0)

$(document).ready(function(){

var delete_id= "<?php echo $item['id'] ; ?> "; 
var session_id="<?php echo $_SESSION['code'] ;?>";
$.ajax({
       type:"post",
       datatype:"html",
       url:"cart.php",
       data:"delete_id="+delete_id+"&session_id=session_id,
       success:(data){
          /*put return data here */
      }



    })

现在制作cart.php页面

     <?php
       $delete_id=$_POST['delete_id'];
       $ses_id=$_POST['session_id'];

       now perform your sql query 
   ?>