Hi Team
I am debugging an error log and notice when inserting a data from both client side(javascript and html). Its expecting some additional fields from the database. My connection is fine, but if it says " must include all my table defitions from wishlist or products?
{
"success": false,
"message": "Required data missing."
}
// html code
JavaScript
Colorful Stylish Shirt
R200.00
R200.00
View Detail
Add To Wishlist
/***
@author:Gcobani Mkontwana
@date:11/10/2023
@script handles adding of wishlist to cart.
**/
$(document).ready(function () {
// Initialize wishlist count to 0
let wishlistCount = 0;
// Function to update the wishlist badge count
function updateWishlistBadge() {
$("#wishlist-badge").text(wishlistCount);
}
// Function to open the login modal when the badge is clicked
function openLoginModal() {
$("#wishlistLoginModal").modal("show");
}
// Function to display wishlist items
function displayWishlistItems() {
// Here, you should make an AJAX request to your server to fetch the user's wishlist items.
// Replace the following code with your actual request:
$.ajax({
url: 'get-wishlist-product.php', // Replace with your API endpoint
method: 'GET',
dataType: 'json',
success: function (response) {
if (response.success) {
// The server should return an array of wishlist items.
const wishlistItems = response.items;
// Clear the existing items in the list
$('#wishlistItems').empty();
// Add each item to the list
wishlistItems.forEach(function (item) {
$('#wishlistItems').append(`${item.product_name}
`);
});
}
},
error: function (error) {
console.error('Error fetching wishlist items:', error);
}
});
}
// Listen for the "Add to Wishlist" button click
$(".add-to-wishlist").click(function () {
// Simulate adding an item to the wishlist
const productID = $(this).data("id");
const productName = $(this).data("product-name");
const productImage = $(this).data("product-image");
//const productCode = $(this).data("product_code");
// You can now send this product information to the server using an AJAX request to add it to the user's wishlist.
// Replace the following code with your actual request:
$.ajax({
l: 'add-to-wishlist.php', //
method: 'POST',
data: { product_id: productID, product_name: productName, product_image: productImage },
dataType: 'json',
success: function (response) {
if (response.success) {
// If the product is successfully added to the wishlist, update the badge count
wishlistCount++;
updateWishlistBadge();
// If items are in the wishlist, open the login modal and display items
if (wishlistCount > 0) {
displayWishlistItems();
openLoginModal();
}
} else {
console.error('Failed to add to wishlist:', response.message);
}
},
error: function (error) {
console.error('Error adding to wishlist:', error);
}
});
});
});// server side
PHP
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Check if the product exists in the products table based on name, image, and code
$productQuery = "SELECT id FROM products WHERE product_name = :product_name AND product_image = :product_image ";
$productStmt = $pdo->prepare($productQuery);
$productStmt->bindParam(":product_name", $product_name, PDO::PARAM_STR);
$productStmt->bindParam(":product_image", $product_image, PDO::PARAM_STR);
$productStmt->execute();
if ($productStmt->fetch()) {
// The product exists, so it's safe to add it to the wishlist
$query = "INSERT INTO wishlist (user_id, product_name, product_id) VALUES (:user_id, :product_name, :product_image)";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam(":product_name", $product_name, PDO::PARAM_STR);
$stmt->bindParam(":product_image", $product_image, PDO::PARAM_STR);
$stmt->execute();
$response = array("success" => true, "message" => "Product added to your wishlist.");
} else {
$response = array("success" => false, "message" => "Product does not exist.");
}
} catch (PDOException $e) {
// Handle database connection or query errors
$response = array("success" => false, "message" => "Database error: " . $e->getMessage());
}
} else {
// If required POST values are missing
$response = array("success" => false, "message" => "Required data missing."); // issue is here
}
// Return a JSON response
header("Content-Type: application/json");
echo json_encode($response);
ob_end_flush(); // End output buffering
?>
Tahir AnsariRank #366 · 6.8K reputationPosted Oct 13, 2023, 6:12 AM EDT
Make sure that the server-side code (
add-to-wishlist.php) is correctly handling the incoming POST request. Check if it expects and processes theproduct_id,product_name, andproduct_imagedata correctly. Ensure that the server-side code is set up to save this data into the database or perform the necessary actions.please check the below code why are you using l
$.ajax({ l: 'add-to-wishlist.php', // method: 'POST', data: { product_id: productID, product_name: productName, product_image: productImage }, dataType: 'json',
By changing
"l"to"url"in the$.ajaxcall, you should be able to send the AJAX request to the 'add-to-wishlist.php' script correctly. This change should resolve the issue you're facing with your AJAX request.