mardi 23 février 2016

Store inputs in a 'cart' array

I need some help storing an order in a 'cart' array. I just can not get this to work.

Please see my code below.

PHP:

session_start();

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

## Add
if (isset($_POST['Submit']) && $_POST['Submit'] == 'add' ) {
    $ticket = $_POST['courseID'];
    add($ticket);
    header('Location: book.php');
}   

## Add to Cart
function add($ticket) {
    $_SESSION['cart'][$ticket['courseID']] = $ticket;
    $_SESSION['cart'][$ticket['courseID']]['bday'] = $_POST['day'];
    $_SESSION['cart'][$ticket['courseID']]['btime'] = $_POST['time'];
    $_SESSION['cart'][$ticket['courseID']]['badult'] = $_POST['as'];
    $_SESSION['cart'][$ticket['courseID']]['bchild'] = $_POST['cs'];
    return TRUE;

}

Form:

<h2><?php echo $_POST['title'];?></h2>

<form id="booking" onSubmit="return check_book()" method="post" action="#">
    <!-- Course Name -->
    <input type="hidden" name="courseID" id="cID" value="<?php echo $_POST['courseID'];?>"/>
    <!-- Course Day -->
    <input type="text" name="day" id="day" value="<?php echo $_POST['day'];?>" readonly/>
    <!-- Course Time -->
    <input type="text" name="time" id="time" value="<?php echo $_POST['time'];?>" readonly/>

    <!-- Adult Spots -->
    <select name="as" class="1-10">
        <option value="" name="adult" disabled selected>Adult</option>
    </select>
    <input type="text" class="as_price" readonly/>
    <!-- Child Spots -->
    <select name="cs" class="1-10">
        <option value="" name="child" disabled selected>Child</option>
    </select>
    <input type="text" class="cs_price" readonly/>

    <!--Submit Button -->
    <input type="submit" value="Add to Cart"/>

</form>

I would also like to be able to increase / decrease the tickets as well, but I am just trying to get the basics to work now.

Any help would be greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire