将<form>输入存储在'cart'数组中

时间:2016-02-23 12:01:08

标签: php html web

我需要一些帮助,在“购物车”数组中存储订单<form>。我无法让它发挥作用。

请参阅下面的代码。

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;

}

形式:

<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>

我也希望能够增加/减少门票,但我只是想让基础知识现在正常运作。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您正在检查是否存在$ _POST ['Submit'],但这不存在。

如果您检查表单,则没有任何名为“提交”的表单字段 - 您需要更改PHP逻辑而不是检查它,或者更改HTML表单以包含它:

<!--Add an extra hidden form field called 'Submit' with a value of add: -->
<input type="hidden" name='Submit' value="add"/>