$ _Post请求和刷新

时间:2015-03-12 20:21:27

标签: php post

我的菜单应用程序有问题。它应该允许订购(饮料或晚餐)并从少数选择中选择。 (我必须在没有数据库或任何外部文件的情况下这样做,这就是为什么我的代码在1个文件中并包含css-sory) 不幸的是,当我填充我的桌子$ order时,它被填满后就会变空了。我知道它与工作_Post相关但我不知道如何躲闪这个。有任何想法吗?

<?php
$order[10] = new Dish;
$i=0;
class Dish
{
    private $Price;
    private $Name;
    public function setData($Price, $Name)
    {
        $this->Price = $Price;
        $this->Name = $Name;
    }

    function getPrice()
    {
        return $this->Price;
    }

    function getName()
    {
        return $this->Name;
    }
}
$Polish_Dinner = new Dish;
$Polish_Dinner->setData("3$", "Kapusniak");
$Polish_Desser = new Dish;
$Polish_Desser->setData("3$", "Kapusniak z cukrem");


$Mexican_Dinner = new Dish;
$Mexican_Dinner->setData("4$", "Fasola");
$Mexican_Desser = new Dish;
$Mexican_Desser->setData("4$", "Fasola z cukrem");

$Italy_Dinner = new Dish;
$Italy_Dinner->setData("5$", "Pizza");
$Italy_Desser = new Dish;
$Italy_Desser->setData("5$", "Pizza z cukrem");

$Ice = new Dish;
$Ice->setData("0.5$", "Ice");

$Lemon = new Dish;
$Lemon->setData("0.5$", "Lemon");

$Drink = new Dish;
$Drink ->setData("20$","Just Drink");
?>
<div style="background-color: gray;">

<form action ="index.php" method="post">
            <input type="submit" value="Dinner" name="Dinner" style="margin: 35px 100px; display: inline;  ;width: 20%; height: 300px" />
            <input type="submit" value="Cash" name="Cash" style="display: inline;  width: 10%; height: 75px" />
            <input type="submit" value="Name" name="Name" style="display: inline;  width: 10%; height: 75px" />
            <input type="submit" value="Drink" name="Drink" style="margin: 35px 0 0 100px   ; display: inline; width: 20%; height: 300px"/></br>
</form>
</br></br></br>
<?php
if(isset($_POST['Dinner']))
{
    ?>
    <form action ="index.php" method="post" style=" background-color: darkgreen;">
        <input type="submit" value="Mexican" name="Mexican" style="margin-left:100px; display: inline; width: 20%; height: 100px" />
        <input type="submit" value="Polish" name="Polish" style="margin-left:100px;display: inline;  width: 20%; height: 100px" />
        <input type="submit" value="Italy" name="Italy" style="margin-left: 100px; display: inline;   width: 20%; height: 100px"/></br>
    </form>
<?php
}
elseif(isset($_POST['Drink'])) 
{
    $order[$i]=$Drink;
    $i++;
?>
    <form action ="index.php" method="post" style="background-color: darkgreen;">
        <input type="submit" value="Extra ice" name="ice" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
        <input type="submit" value="Extra lemon" name="lemon" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
        <input type="submit" value="Extra Ice + Lemon" name="iclem" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
    </form>
<?php
}

if(isset($_POST['Mexican'])) {
    ?>
    <form action ="index.php" method="post" style="background-color: darkblue;">
        <input type="submit" value="Mexican Lunch" name="Mex_Lunch" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
        <input type="submit" value="Mexican Lunch + Desser" name="Mex_Desser" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
    </form>
    <?php
} else if(isset($_POST['Polish'])) {
     ?>
    <form action ="index.php" method="post" style="background-color: darkblue;">
        <input type="submit" value="Polish Lunch" name="Pol_Lunch" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
        <input type="submit" value="Polish Lunch + Desser" name="Pol_Desser" style="margin-left: 100px; display: inline;width:20%; height: 100px" />
    </form>
    <?php
} else if(isset($_POST['Italy'])) {
    ?>
    <form action ="index.php" method="post" style="background-color: darkblue;">
        <input type="submit" value="Italy Lunch" name="It_Lunch" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
        <input type="submit" value="Italy Lunch + Desser" name="It_Desser" style="margin-left: 100px; display: inline;width: 20%; height: 100px" />
    </form>
    <?php
} else if(isset($_POST['Mex_Desser'])) {
    $order[$i]=$Mexican_Desser;
    $i++;
    $order[$i]=$Mexican_Dinner;
    $i++;
} else if(isset($_POST['Mex_Lunch'])) {
    $order[$i]=$Mexican_Dinner;
    $i++;
} else if(isset($_POST['Pol_Desser'])) {
    $order[$i]=$Polish_Desser;
    $i++;
    $order[$i]=$Polish_Dinner;
    $i++;
} else if(isset($_POST['Pol_Lunch'])) {
    $order[$i]=$Polish_Dinner;
    $i++;
} else if(isset($_POST['It_Desser'])) {
    $order[$i]=$Italy_Desser;
    $i++;
    $order[$i]=$Italy_Dinner;
    $i++;
} else if(isset($_POST['It_Lunch'])) {
    $order[$i]=$Italy_Dinner;
    $i++;
}else if(isset($_POST['Ice'])) {
    $order[$i]=$Ice;
    $i++;
}else if(isset($_POST['Lemon'])) {
    $order[$i]=$Lemon;
    $i++;
}
else if(isset($_POST['iclem'])) {
    $order[$i]=$Lemon;
    $i++;
    $order[$i]=$Ice;
    $i++;
}
if(isset($_POST['Cash'])) {
    if($i==0)
        echo("Make your order first!");
    else
        echo $order[$i]->getPrice();
}
if(isset($_POST['Name'])) {
    if($i==0)
        echo("Make your order first!");
    else
        echo $order[$i]->getName();
}
?>

</div>

..........

2 个答案:

答案 0 :(得分:1)

如果要将订单结果复合到数组中,则必须在将变量赋值给数组之前为其赋值:

$Polish_Dinner = $_POST['Pol_Desser'];

或者,只需为数组索引指定适当的值:

$order[$i] = $_POST['Pol_Desser'];

要在多个提交中携带变量,请指定为会话变量。

如果您不能使用SESSION变量,那么您可以使用$ _GET,但不建议这样做。

答案 1 :(得分:0)

我有一些示例代码可能很有用但是在这里直接共享太长了。我把它放在这里:

http://tny.cz/c681c8c9

我以前遇到过类似的问题,但确实觉得这很有帮助。不幸的是,我找不到它所共享的网站的原始链接。如果您需要任何解释,请告诉我

<强>更新

  $store = Array().         
  $order = **what you want to store**
  array_push($store, $order);
  $_SESSION['array'] = $store;

然后您可以访问另一个区域中的数组内容