私は衣料品ショッピングのウェブサイトを実装していますが、現在カートに追加しています。すべての布アイテムにカートに追加ボタンがあり、アイテムがカートに追加されます。私はカートに値を格納するためにセッション配列を使用しています。私は、カートボタンに追加すると「カートに追加されたアイテム」というメッセージを表示するためにjavascriptアラートを使用しています。私は問題を直面しているときに、私はカートのボタンに最初にそれは任意のメッセージを表示しないが、それは完璧に2番目と他のすべての時間の作品を追加するをクリックします。最初のクリックでのみ何も表示されません。JavaScriptの警告がPHPの最初の配列の値で機能しない
コード:
<?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">
へ 変更コードだけでなく、あなたのjavascriptを投稿してください! – BCoder
JavaScriptのアラートを使用すると、JavaScriptのエコーをどこに表示しているのかわかります –
セッション 'isset($ _ SESSION [" shopping_cart "])'が設定されておらず、 'if'の状態になっていません –