jeudi 3 novembre 2016

PHP not storing values of checkbox

I am making a fake tool rental program using a database and php. This code gets information from a database stores it in $records then draws a table with the information. I am trying to store the tool_id from the checkboxes into an array to make a cart but the array is always empty.

function drawTable($records){
$count = 0;
$info = getToolInfo($records);

echo "<table style='text-align:center;margin:0 auto;border:1px solid black'>";
    echo "<tr style='border:1px solid black'>";
        echo "<td style='border:1px solid black'>Add to cart</td>";
        echo "<td style='border:1px solid black'>Tool Name</td>";
        echo "<td style='border:1px solid black'>Price</td>";
        echo "<td style='border:1px solid black'>Availability</td>";
        echo "<td style='border:1px solid black'>More info</td>";
    echo "</tr>";
    foreach($records as $record){
        echo "<tr stlye='border:1px solid black'>";

            echo "<td style='border:1px solid black'><input type='checkbox' name='cart[]' value=".$record['tool_id']."></td>";
            echo "<td style='border:1px solid black'>".$record['name']."</td>";
            echo "<td style='border:1px solid black'>$".$record['price_total']."</td>";
            echo "<td style='border:1px solid black'>".$record['status']."</td>";
            echo "<td style='border:1px solid black'><button tpye='button' onclick='var info=".json_encode($info[$count]).";displayPopup(info);'>More Info</button></td>";

        echo "</tr>";
        $count++;
    }
echo "</table>";
}

This code is where I try to access to array in another file called cart.php but the array is always empty.

<?php
session_start(); 

if (!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array();
}

$cart = $_GET['cart'];

foreach($cart as $tool){
    $_SESSION['cart'][] = $tool;
    echo $tool . "<br/>";
}


?>




Aucun commentaire:

Enregistrer un commentaire