I am implementing clothing shopping website and currently i am working on add to cart. on every cloth item there is add to cart button through which item is added to cart. i am using session array for storing values in cart. i am using javascript alert to show message "Item added to cart" when add to cart button is clicked. i am facing problem that when i click add to cart button first time it doesn't show any message but it works perfectly for 2nd and all other time. only on first click it shows nothing.
Code:
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "login");
if (isset($_POST["add_to_cart"])) {
if (isset($_SESSION["shopping_cart"])) {
$item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
if (!in_array($_GET["id"], $item_array_id)) {
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'item_id' => $_GET["id"],
'item_description' => $_POST["dress_description"],
'item_price' => $_POST["price"],
'item_quantity' => $_POST["dress_quantity"],
'item_gender' => $_POST["gender_name"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
echo '<script>alert("Item Added to Cart")</script>';
echo '<script>window.location="portfolionew.php"</script>';
} else {
echo '<script>alert("Item Already Added")</script>';
echo '<script>window.location="portfolionew.php"</script>';
}
} else {
$item_array = array(
'item_id' => $_GET["id"],
'item_description' => $_POST["dress_description"],
'item_price' => $_POST["price"],
'item_quantity' => $_POST["dress_quantity"],
'item_gender' => $_POST["gender_name"]
);
$_SESSION["shopping_cart"][0] = $item_array;
echo '<script>alert("Item Added to Cart")</script>';
echo '<script>window.location="portfolionew.php"</script>';
}
}
?>
// and this is cart button:
<input type="submit" name="add_to_cart" class="fa fa-shopping-cart" value="Cart">