2017-08-17 1 views
0

Ich versuche also, Einkaufswagen Artikel von dort ID zu speichern. Sobald dies passiert, muss ein Button "Remove from Cart" erscheinen, wenn ich diesen Button anklicke erscheint ein neuer Button "Add to Cart" aber ich bin von diesem Punkt an etwas verloren. Ich bin neu hier, bitte entschuldige meine Fehler. Mein Code ist unten:

session_start(); 

$items[] = $_POST['add']; 

if (isset($_POST['add'])) { 
    $_SESSION['cart'][]=$_POST['add']; 

} else if (isset($_POST['remove'])) { 

unset ($_SESSION['cart'][$_POST['remove']]); 
} 


foreach($vend as $vendID=> $items) { 
echo "<form action='vend.php' method='post'>"; 
echo "<article id ='vend-$vendID'>"; 
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>"; 
echo "<div class ='item-no'>"; 
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>"; 
echo "</div>"; 
echo "<div class ='img-div'>"; 
echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>"; 

echo "<div class='item-p'>"; 
echo "<p>{$items['desc']}</p></div>"; 

echo "<div class='pricing'>"; 
echo "<p><b>Price: $</b>{$items['price']}</p></div>"; 
//echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>"; 


if(isset($_POST['add']) && ($_SESSION['cart'] == $vendID)) { 

echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>"; 
} 


else { 


echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>"; 

}  
+0

mir sieht aus, als dieser Beitrag würde Ihre Frage beantworten [html-php-Form-input-as-Array] (https://stackoverflow.com/questions/20184670/html-php-form-input-as-array) – chilly

Antwort

0

hier In Ihrem Fall, was getan werden sollte:

session_start(); 
/* Check if $_SESSION['cart'] exists */ 
if (!isset($_SESSION['cart'])) { 
    /* Init cart as empty array */ 
    $_SESSION['cart'] = []; 
} 

if (isset($_POST['add']) && 0 < $_POST['add']) { 
    /* Add product id to session cart */ 
    $_SESSION['cart'][$_POST['add']] = 1; 
} else if (isset($_POST['remove']) && 0 < $_POST['remove']) { 
    /* Remove product id from session cart */ 
    unset($_SESSION['cart'][$_POST['remove']]); 
} 
// I check with `0 < $id` because ids are always positive 

foreach($vend as $vendID=> $items) { 
    echo "<form action='vend.php' method='post'>"; 
    echo "<article id ='vend-$vendID'>"; 
    echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>"; 
    echo "<div class ='item-no'>"; 
    echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>"; 
    echo "</div>"; 
    echo "<div class ='img-div'>"; 
    echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>"; 

    echo "<div class='item-p'>"; 
    echo "<p>{$items['desc']}</p></div>"; 

    echo "<div class='pricing'>"; 
    echo "<p><b>Price: $</b>{$items['price']}</p></div>"; 

    if(isset($_SESSION['cart'][$vendID])) { 
     // you have `$vendID` in session cart - then show Remove button 
     echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>"; 
    } else { 
     // you DON'T have `$vendID` in session cart - then show Add button 
     echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>"; 
    } 
    // Also don't forget to close `</form>`  
} 
+0

Wenn ich auf " In den Warenkorb“nichts scheint –

+0

Versuchen passieren zu ersetzen' button' zu 'input type = 'submit'' –

+0

Noch nicht funktioniert :( –

0

Um einen Artikel aus Ihrem Warenkorb zu entfernen, greifen Sie auf das Element basierend auf Ihrer Variablen $ vendID zu. Welche Ihre Artikel erfordern würde in $_SESSION['cart'] als Array gespeichert werden:

$_SESSION['cart'][0] = "item 1"; 
$_SESSION['cart'][1] = "item 2"; 
... 

aber später in Ihrem Code Sie $_SESSION['cart'] zugreifen, als ob es nur ein Wert kein Array ist

ein Element in Ihrem Code hinzufügen wird mit der folgenden Zeile getan:

$_SESSION['cart'][]=$_POST['add']; 

Dies führt in etwa wie folgt:

//empty cart 
$_SESSION['cart'][0] = $vendid; 
0

Stellt sich heraus, ich einen Unterstrich benötigt, hatte ich $ POST anstelle von $ _POST und es funktioniert jetzt, bedankt sich bei allen :)

Verwandte Themen