lundi 4 février 2019

DOM is not updating, even with .on

Project E-commerce website in Laravel.

The DOM is not loading. I have added my codes below. I can add products all I want but when I go to delete them it doesn't work. The DOM is not updating until I refresh the browser or page or move to another page. I have been through many solutions on the web, but most are saying I need to use .on instead of .click and .bind and I've tried it and still deosn't work.

TIA.

Load.js

    function update_cart() {
   // alert(js_url);
    $.ajax({
        url: js_url + "update_cart",
        type: "GET",
        success: function(data) {
           // alert(data);
            $("#boro_cart").html(data);


        }
    });
}

$(document).ready(function() {
    $(".delete_list_cart").on("click", function(e) {

        var list_id = $(this).attr("id");

        $("#list_" + list_id).remove();
        $.ajax({
            url: js_url + "delete_list_cart",
            type: "POST",
            data: {
                get_content: list_id
            },
            timeout: 60000,
            success: function(data) {
                update_cart();
            }
        });
        e.preventDefault();
    });

    $(".add_to_basket").click(function(e) {

        var get_content = $(this).attr("data-bind");


       $.ajax({
            url: js_url + "add_basket",
            type: "POST",
            data: {
                get_content: get_content
            },
            timeout: 60000,
            success: function(data) {
                update_cart();
            }
        });
        e.preventDefault();
    });

    $(".increase_qty").on("click", function(e) {
        var get_content = $(this).attr("data-bind");

        $.ajax({
            url: js_url + "increase_qty",
            type: "POST",
            data: {
                get_content: get_content
            },
            timeout: 60000,
            success: function(data) {
                alert(data);
                update_cart();
            }
        });
        e.preventDefault();
    });

    $(".decrease_qty").on("click", function(e) {
        var get_content = $(this).attr("data-bind");

        $.ajax({
            url: js_url + "decrease_qty",
            type: "POST",
            data: {
                get_content: get_content
            },
            timeout: 60000,
            success: function(data) {
                update_cart();
            }
        });
        e.preventDefault();
    });
});

CartController.php

class CartController extends Controller
{
    //
   public function addBasket() {


    if ($_POST["get_content"]) {
        list($product_id, $product_price, $product_name) = explode("::", $_POST["get_content"]);

        if (isset($_COOKIE["myCookie"])){
            //pr("INN");
            $store = json_decode($_COOKIE["myCookie"], true);
            $record_found = false; 

            foreach ($store as $key=>$data) {
                if ($product_id == $data["id"]) {
                    $store[$key]["qty"] = $data["qty"] + 1;
                    $record_found = true;
                    break;
                }
            }
            if ($record_found == false) {
                $store[] = array("id"=>$product_id, "price"=>$product_price, "name"=>$product_name, "qty"=>1);
            }

            unset($_COOKIE['myCookie']);
            setcookie('myCookie', null, -1, '/'); /* for deleting cookie data*/
            setcookie("myCookie", json_encode($store), time() + (86400 * 30), "/"); // 86400 = 1 day
           // pr($store);
        } else {
            $store[] = array("id"=>$product_id, "price"=>$product_price, "name"=>$product_name, "qty"=>1);
             setcookie("myCookie", json_encode($store), time() + (86400 * 30), "/"); // 86400 = 1 day
        }

     }

       exit();

   }
   public function updateCart() {
       loadCart(false);
        exit();
   }


    public function deleteCartList() {
        if (isset($_COOKIE["myCookie"])){
            //pr("INN");
            $store = json_decode($_COOKIE["myCookie"], true);
            $record_found = false;

            foreach ($store as $key=>$data) {
                if ($_POST["get_content"] == $data["id"]) {
                    unset($store[$key]);
                    $record_found = true;
                    break;
                }
            }

    if ($record_found == true) {

        unset($_COOKIE['myCookie']);
        setcookie('myCookie', null, -1, '/');
        setcookie("myCookie", json_encode($store), time() + (86400 * 30), "/"); // 86400 = 1 day
    }
        }
        exit();
    }

    public function increaseQty() {
       //pr($_POST);
        if (isset($_COOKIE["myCookie"])){
            //pr("INN");
            $store = json_decode($_COOKIE["myCookie"], true);
            $record_found = false;
            list($product_id, $product_opr) = explode("::", $_POST["get_content"]);
            foreach ($store as $key=>$data) {
                if ($product_id == $data["id"]) {
                    if ($product_opr == "increase") {
                        $store[$key]["qty"] =  $store[$key]["qty"] + 1;
                        $record_found = true;
                    }
                    break;
                }
            }

            if ($record_found == true) {

                unset($_COOKIE['myCookie']);
                setcookie('myCookie', null, -1, '/');
                setcookie("myCookie", json_encode($store), time() + (86400 * 30), "/"); // 86400 = 1 day
            }
        }
       exit();
    }

    public function decreaseQty() {
        //pr($_POST);
        if (isset($_COOKIE["myCookie"])){
            //pr("INN");
            $store = json_decode($_COOKIE["myCookie"], true);
            $record_found = false;
            list($product_id, $product_opr) = explode("::", $_POST["get_content"]);
            foreach ($store as $key=>$data) {
                if ($product_id == $data["id"]) {
                    if ($product_opr == "decrease") {
                        $store[$key]["qty"] =  $store[$key]["qty"] - 1;
                        $record_found = true;
                    }
                    break;
                }
            }

            if ($record_found == true) {

                unset($_COOKIE['myCookie']);
                setcookie('myCookie', null, -1, '/');
                setcookie("myCookie", json_encode($store), time() + (86400 * 30), "/"); // 86400 = 1 day
                // pr($store);
            }
        }
        exit();
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2G7v9iJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire