当用户更改他/她购买的产品数量时,我无法在数据库(使用ajax!)更新我的购物篮?

时间:2016-12-23 17:29:58

标签: php jquery mysql ajax

我有一个电子商店。在用户篮子中,用户添加了一些产品。现在他/她想要改变(增加或减少)产品的数量(数量)。我用ajax写了这个部分,但它没有用。一切似乎都对!我不知道什么是错的!请帮帮我......

"最后我添加了" update_order.php"页面代码。"

   <?php
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

    include('database.php');
    $object=new myclass_parent;

    ?>
    <html>
        <head>
            <meta charset="utf-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <script src="js/jquery.min.js"></script>
            <script src="js/bootstrap.min.js"></script>
            <link rel="stylesheet" type="text/css" href="css/style.css" />
            <link href="css/bootstrap.min.css" rel="stylesheet"/>
            <link href="css/bootstrap.rtl.min.css" rel="stylesheet"/>
            <script type="text/javascript">
                $(document).ready(function () {
                    $(".navbar-right li .navbar-brand").hide();
                });
            </script>
            <title></title>
        </head>
        <body>

            <?php

            include('header.php');

            ?>


            <!--------------------------------->
            <!--------------------------------->
            <!--------------------------------->
            <!--------------------------------->
            <!--------------------------------->


            <div class="container content info">
                <h4>سبد خرید</h4>

                <div class="row">

                <div style="display:none;margin-top:10px;" id="err_alert" class="alert alert-danger ">
                                <span id="empty_err"> </span> 

                 </div> 

                    <div class="table-responsive">          
                      <table class="table my_table">
                        <thead>
                          <tr>
                            <th>فروشنده</th>
                            <th>توضیحات کالا</th>
                            <th>تعداد</th>
                            <th>قیمت واحد</th>
                            <th colspan="2">قیمت کل</th>
                          </tr>
                        </thead>
                        <tbody>



                        <?php


                        if(isset($_COOKIE['mybasket'])){



                            $query="select * from tblbasket where cookiename=? and payment=0";
                            $array=[$_COOKIE['mybasket']];
                            $result_array=$object->select($query,$array);

                            foreach($result_array as $result){

                                $productid=$result['productid'];
                                $count=$result['count'];

                                $query2="select * from tblproducts where id=?";
                                $array2=[$productid];
                                $result_array2=$object->select($query2,$array2);

                                $img=$result_array2[0]['img'];
                                $title=$result_array2[0]['title'];
                                $price=$result_array2[0]['price'];
                                $info=$result_array2[0]['info'];
                                ?>

                                <tr id="<?php echo $productid; ?>" class="pitem">

                                <td rowspan=''><a href="http://amazoon.com">amazoon.com</a></td>

                                     <td>
                                        <img src="<?php echo $img; ?>" width="100" height="80"/>
                                        <div>
                                            <strong><?php echo $title; ?></strong>
                                            <p><?php echo $info; ?></p>
                                        </div>
                                     </td>
                                <td>
                                <input style="width:50px" type="number" id="pcount" class="pcount" 
                                onKeyDown="return checknumber(event);"  min="1" value="<?php  echo $count; ?>">
                                </td>
                                <td><span id="pprice"><?php echo $price; ?></span> تومان</td>
                                <td><span id="kol_price"><?php echo $count*$price; ?></span> تومان</td>
                                <td><a style="cursor:pointer;" class="remove"><img src="images/delete.png" alt="حذف محصول" title="حذف محصول"/></a></td>';


                        <?php       
                            }//foreach
                         ?>





                        </tr><!--basket_items-->


                        </tbody>
                      </table>



                      <br/>
                      </div>
                </div>

                <?php

                        }//isset cookie


                        ?>


                <div class="row">
                <div class="col-12">
                    <table class="table-responsive tbl jamkharid">
                        <thead>
                        <tr><th>پیش فاکتور خرید</th></tr>
                        <tr><th>مبلغ کل</th><th>مالیات 9%</th><th>هزینه ارسال</th>           <th>مبلغ پرداختی نهایی</th></tr>
                        </thead>
                        <tbody>
                        <td><span id="jam_kharid"></span> تومان</td><td><span id="maliat"></span> تومان</td>
                        <td><span id="delivery">0</span> تومان</td><td><span id="jam_kharid_final"></span> تومان</td>
                        </tbody>

                    </table>
                </div><!--col12-->
                </div><!--row-->

                <div class="row">
                    <?php if(isset($_SESSION['email'])){ ?>

                    <a href="info.php" class="button">مرحله بعد</a>

                 <?php }//if
                        else{
                  ?>

                  <a href="login_register.php" class="button">مرحله بعد</a>

                  <?php }//else ?>
                </div>
            </div>
            <!--------------------------------->
            <!--------------------------------->
            <!--------------------------------->
            <!--------------------------------->
            <!--------------------------------->
            <?php
            include('footer.php');
            ?>
        </body>
        </html>

        <script>

        var timer;

       function jam_kharid(){

        var amount=0;
        var buy_amount=0;

        clearTimeout(timer);
        timer=setTimeout(function(){

            $(".pitem").each(function(index, element) {


                var pcount=$(this).find("#pcount").val();
                var pprice=$(this).find("#pprice").text();

                amount=amount+parseInt(pcount)*parseInt(pprice);    

                buy_amount=(1.09*amount).toFixed(0);

            });//each pitem function


                $(".pitem .pcount").change(function(){

                    var pcount=$(this).val();
                    var pprice=$(this).parents(".pitem").find("#pprice").text();

                    var kol_price=parseInt(pcount)*parseInt(pprice);
                    $(this).parents(".pitem").find("#kol_price").text(kol_price);

                    jam_kharid();
    //****************************************************************************************************************      

                    var productcount=pcount;
                    var productid=$("#pcount").parents(".pitem").attr('id');

                    alert(productid);

                    $.ajax({

                        url:'update_order.php',
                        type:'POST',
                        data:{productid:productid,cookiename:<?php echo $_COOKIE['mybasket']; ?>,productcount:productcount}

                    })//ajax
                    .done(function(msg){

                        alert(msg);

                    })//done

    //****************************************************************************************************************              
                });//change



                $(".jamkharid").find("#jam_kharid").html(amount);

                $('.jamkharid').find("#maliat").text(amount*0.09);

                $('.jamkharid').find("#jam_kharid_final").text((1.09*amount).toFixed(0));



        },600);

    }//jam_kharid


    jam_kharid();




    function khali(){
    var l=$('.pitem').length;

    if(l==0){

        $('#err_alert').show(300); $('#err_alert #empty_err').show(); $('#err_alert #empty_err').html('هیچ محصولی در سبد خرید شما موجود نمی باشد!');

        $('.my_table').hide();

        $('.jamkharid').hide();
    }

    }//function p_length

    khali();

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

        var id_pitem=$(this).parents(".pitem").attr('id');
        var pitem_parent=$(this).parents(".pitem");

        $.ajax({

            url:'delete.php',
            type:'POST',
            data:{id:id_pitem}

        })//ajax
        .done(function(){

            pitem_parent.remove();

            alert('محصول مورد نظر حذف شد');

            jam_kharid();
            khali();

        })//done

    })//remove click function



    function checknumber(e){

        if( (e.keyCode==65 && e.ctrlKey) || (e.keyCode>=35 && e.keyCode<=40) || ($.inArray(e.keyCode,[8,46])!=-1) ){

            return;

        }//if

        if( e.shiftKey || ((e.keyCode<48 || e.keyCode>57) && (e.keyCode<96 || e.keyCode>105)) ){
            e.preventDefault();
        }//if

    }//checknumber

    </script>

////////////////////////////////////////////////////////////////////////////////
update_order.php :
////////////////////////////////////////////////////////////////////////////////
<?php
 include('database.php'); 
$object=new myclass_parent; 
$product_id=intval($_POST['productid']);
 $cookie_name=$_POST['cookiename']; 
$product_count=intval($_POST['productcount']);

 $query_update="update tblbasket set count=? where cookiename=? and productid=? and payment=0"; 
$array=array($product_count,$cookie_name,$product_id);
 $object->myquery($query_update,$array); echo $product_id;

 ?>

0 个答案:

没有答案