Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

jquery - Can I use JQ fetch url to pass data to a pre-defined PHP class?

I am creating a restaurant webpage. Here I want to add a "add to cart" button using form. The form submission will not refresh the page. So I have used JQ fetch. Everything is working just fine, but I want to pass my data to a pre-defined class. Here I am using MVC framework. Here is my code...

Model->Home.php...

    public function add_cart() {
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        if(isset($_SESSION['is_logged_in'])){
            $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
            if (isset($post['add_cart'])) {
                if(isset($_SESSION['cart']) && $_SESSION['cart']) {
                    if(!in_array($post['food_id'], $_SESSION['cart'])) {
                        array_push($_SESSION['cart'], $post['food_id']);
                        ?>
                        <script>console.log("hello");</script>
                        <?php
                        $this->query('UPDATE customer SET cart=:cart WHERE mobile_number=:mobile_number');
                        $this->bind(':cart', array_management::serializeArray($_SESSION['cart']));
                        $this->bind(':mobile_number', $_SESSION['user_id']);
                        $this->execute();
                    }
                } else {
                    $_SESSION['cart'] = array();
                    array_push($_SESSION['cart'], $post['food_id']);

                    $this->query('UPDATE customer SET cart=:cart WHERE mobile_number=:mobile_number');
                    $this->bind(':cart', array_management::serializeArray($_SESSION['cart']));
                    $this->bind(':mobile_number', $_SESSION['user_id']);
                    $this->execute();
                }
            }
            // header('Location: '.ROOT_PATH, false);
        } else {
            header('Location: '.ROOT_URL.'users/login#login');
        }
    }
}

JQ

$("form").submit(function(){
    var submit_button = $(this).find("[type=submit]");

    if(!submit_button.hasClass('disabled')) {
        let var_form_data = new FormData(this);
        var_form_data.append = $(this).serialize();

        // console.log(var_form_data);
        // fetch("<?php echo ROOT_URL; ?>models/Cart.php", {
        fetch("<?php echo ROOT_URL; ?>models/Cart.php", {
            method: "POST",
            body: var_form_data
        })
        .then(function (response) {
            return response.text();
        })
        .then(function (text) {
            // console.log(var_form_data);
            console.log(text);
        })
        .catch(function (error) {
            console.log(error);
        });
    };
    // $(this).attr("method", "POST");
    // submit_button.addClass("disabled");
    submit_button.attr("value","Added to Cart");
    return false;
});

Views->Home.php

<form>
    <input class="d-none" name="cart_edit" type="text" value="add">
    <input class="d-none" name="food_id" type="text" value="<?php echo $value['food_id'] ?>">
    <input class="btn btn-primary" name="add_cart" type="submit" value="Add to Cart">  
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...