检查PHP中可用的座位数

时间:2015-09-27 02:13:19

标签: php session

这是影院网站的座位预订表格,我需要检查用户选择座位时的座位数。

有3种座位类型(S,F,B),容量分别为40,12,13。

在HTML中我有:

<input name="Stype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input name="Ftype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input name="Btype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input type="submit" name="submit" value="submit" />

然后我用php来获取这样的用户输入:

<?php
session_start();
$SSeats = '';
$FSeats = '';
$BSeats = '';

if(isset($POST['submit']))
{
    //get inputs by method POST
    //pass input data to the next shopping-cart page via SESSION
    $_SESSION['SSeats'] = $SSeats;
    $_SESSION['FSeats'] = $FSeats;
    $_SESSION['BSeats'] = $BSeats;
}
else
{
    $_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
    $_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
    $_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];

    //Then I write above SESSIONs into a file, then output the number of
    //seats available to the HTML code
}
?>

在我测试这段代码之前,我测试过很多其他内容,而SESSIONs中已经有了值,所以代码运行良好。但是,当我重新打开网站并转到此页面时,它会抛出一个异常,即索引'SSeats','FSeats','BSeats'未定义。

该要求还包括在用户查看他们的预订购物车后,他们可以在预订中添加/删除座位。

所以有人可能会建议我修复它的方法或如何使它更容易。

2 个答案:

答案 0 :(得分:2)

您有两个错误:

$POST更改为$_POST

if(isset($_POST['submit']))

您需要$_POST['html_element_name']才能获得值

    $_SESSION['SSeats'] = $_POST['SSeats'];
    $_SESSION['FSeats'] = $_POST['FSeats'];
    $_SESSION['BSeats'] = $_POST['BSeats'];

代码:     

if(isset($_POST['submit']))
{
    //get inputs by method POST
    //pass input data to the next shopping-cart page via SESSION
    $_SESSION['SSeats'] = $_POST['SSeats'];
    $_SESSION['FSeats'] = $_POST['FSeats'];
    $_SESSION['BSeats'] = $_POST['BSeats'];
}
else
{
    $_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
    $_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
    $_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];

    //Then I write above SESSIONs into a file, then output the number of
    //seats available to the HTML code
}
?>

答案 1 :(得分:0)

if(isset)中有拼写错误。 $_POST缺少下划线,并且在定义会话变量时没有调用$_POST变量。你在调用空白变量。

<?php
session_start();
$SSeats = '';
$FSeats = '';
$BSeats = '';

if(isset($_POST['submit'])) //typo fixed here
{
    //get inputs by method POST
    //pass input data to the next shopping-cart page via SESSION
    $_SESSION['SSeats'] = $_POST['SSeats'];
    $_SESSION['FSeats'] = $_POST['FSeats'];
    $_SESSION['BSeats'] = $_POST['BSeats'];
}
else
{
    $_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
    $_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
    $_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];

    //Then I write above SESSIONs into a file, then output the number of
    //seats available to the HTML code
}
?>