Gcobani Mkontwana

Gcobani Mkontwana

  • 565
  • 1.9k
  • 407.5k

Back end does not respond when sending data?

Oct 12 2023 10:10 AM

Hi Team

I need help with my back end, when i debug using a tab from network to see how its responding its not writing any insert to my table. What i want to accomplish, the items do add fine, problem i cant view them to see when i click the wishlist also back end must insert when adding to the table. 

{
  "success": false,
  "message": "An error occurred while adding to the wishlist."
}

<?php
//ob_start(); // Start output buffering to prevent header issues

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

// Check if the user is logged in or not
if (isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
} else {
    $user_id = 0;
}

$response = array("success" => false, "message" => "An error occurred while adding to the wishlist.");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Check if product information is provided
    if (isset($_POST['product_id'], $_POST['product_name'])) {
        $product_id = $_POST['product_id'];
        $product_name = $_POST['product_name'];

        try {
            // Establish a database connection
            $pdo = new PDO("mysql:host=localhost;dbname=dbname;charset=utf8", "username", "password");
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // Prepare and execute the SQL query to insert the product into the wishlist
            $sql = "INSERT INTO wishlist (user_id, product_id, product_name) VALUES (:user_id, :product_id, :product_name)";
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
            $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
            $stmt->bindParam(':product_name', $product_name, PDO::PARAM_STR);
            $stmt->execute();

            $response = array("success" => true, "message" => "Product added to the wishlist successfully.");
            }catch (PDOException $e) {
    // Handle database connection or query errors
    $response = array("success" => false, "message" => "Database error: " . $e->getMessage());
    error_log("Database error: " . $e->getMessage());
}

    }
}

// Return a JSON response
header("Content-Type: application/json");
echo json_encode($response);
?>

// front end

             <a  class="btn border" id="cart-badge-btn">
            <i class="fas fa-heart text-danger"></i>
            <span class="badge" id="wishlist-badge">0</span>
            </a>
            <div id="wishlist-modal" class="modal">
        <!-- Wishlist content goes here -->
    </div>


// javascript
$(document).ready(function () {
        // Initialize the wishlist counter
        let wishlistCount = 0;

        // Add to wishlist button click event
        $(".add-to-wishlist").click(function () {
            let productId = $(this).data("product-id");
            let productName = $(this).data("product-name");
            let productImage = $(this).data("product-image");
            console.log("Product ID:", productId);
            console.log("Product Name:", productName);
            //console.log("Product Code:", productCode);
            console.log("Product Image:", productImage);

            // Add the product to the user's session (you need server-side PHP logic for this)
            // Send an AJAX request to add the product to the session
            $.post("add-to-wishlist.php", {
                product_id: productId,
                product_name: productName,
                product_image: productImage
            }, function (response) {
                // Update the wishlist badge
                wishlistCount++;
                $("#wishlist-badge").text(wishlistCount);
                
                console.log("Product ID:", productId);
                console.log("Product Name:", productName);
                //console.log("Product Code:", productCode);
                console.log("Product Image:", productImage);
            });
        });

        // Wishlist badge click event (show wishlist modal)
        $("#cart-badge-btn").click(function () {
            // Load the user's wishlist from the server using AJAX
            $.get("get-wishlist-product.php", function (wishlistItems) {
                // Update the wishlist modal content with the retrieved data
                $("#wishlist-modal").html(wishlistItems);
                // Show the modal
                $("#wishlist-modal").show();
            });
        });
    });

 


Answers (1)