1
<?php
 session_start();
 include("#nav.php");
 include("dbconnectie.php");
 echo "Plaatsingen: ";
        $query = $db->prepare("SELECT * FROM shop");
        $query->execute();
        $result = $query->fetchALL(PDO::FETCH_ASSOC);
        echo "<table>";
            foreach($result as &$data) {
                echo "<tr>";
                $img = $data['img_url'];
                echo "<td>" . $data["brand"] . "</td>";
                echo "<td>" . $data["model"] . "</td>";
                echo "<td> Condition: " . $data["cond"] . "/100 </td>";
                echo "<td> Prijs: &dollar; " . number_format($data["price"],2,",",".") . "</td>";
                    echo "<td> <img src='$img' width='400' height='300' ></img> </td>";
                echo "<td> Plaatsing nummer: " . $data['id_img'] . "</td>";
                echo "</tr>";
                echo "<br>";
            }
        echo "</table>";
        if(isset($_POST['atc']))
        {
            if($_SESSION['on']){
                $myarray = array('0');
                $addtoarray = $_GET['id'];
                array_push($myarray, $addtoarray);
                $_SESSION['cart'] = $myarray;
                echo "Toegevoogd aan uw winkelmandje.";
                var_dump($_SESSION['cart']);
            }else 
            {
                echo "Log eerst in!";
            }

        }
?>

<html>
    <title>Just for kicks</title>
    <header>
    </header>
    <body>
        <form method='post' action=''>
            Plaatsing nummer invoeren:
             <input type='number' name ='id' value ='id'><br>
             <button type="submit" class="submit" name="atc" value="atc">Winkelmadje</button><br><br>
        </form>
    </body>
</html>

Between line 25 and 31 I'm trying to add numbers to the session array but I'm not sure how, because this is clearly not working. It doesn't add the number you fill in, at the form part. But it appears it doesn't do anything.

1
  • There are a few things wrong here. Your form is POST but you're attempting to retrieve using the GET. You are setting value to id in the form. Remove value ='id'. $_SESSION['on'] is never set so it will never go in there. Commented May 28, 2018 at 19:25

1 Answer 1

1

I trimmed your code down to just the bare minimum and added some inline comments. I made the assumption that the contents of $_SESSION['cart'] is an array.

Please notice:

  1. POST was used consistently
  2. The value of the input was removed (value ='id')
  3. The check for $_SESSION['on'] was removed

Code

<?php
// Start your session.
session_start();

// The form was submitted.
if (isset($_POST['atc'])) {

    // Get the cart so we can append to it.
    // Assuming that the cart is an array.
    $cart = (array)$_SESSION['cart'];

    // Append the user's input to the end of the cart.
    $cart[] = $_POST['id'];

    // Store it in the session.
    $_SESSION['cart'] = $cart;

    // Dump out the session.
    var_dump($_SESSION);
}
?>

<html>
<title>Just for kicks</title>
<header>
</header>
<body>
<form method='post'>
    <label> Plaatsing nummer invoeren:
        <input type='number' name='id'/>
    </label><br>

    <button type="submit" class="submit" name="atc" value="atc">Winkelmadje</button>
    <br><br>
</form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.