Ilrind
Ilrind

Reputation: 1

$_Post request and refresh

I have problem with my Menu application. It should allow to make order ( drink or dinner) and chose from few options. ( I have to do it without database or any external files thats why my code is in 1 file and includes css - sory for that) Unfortunately when i fill my table $order with something right after its filled it gets empty. I know its connected with working _Post but i have no idea how to dodge this. Any ideas?

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

..........

Upvotes: 0

Views: 70

Answers (2)

underflow
underflow

Reputation: 483

I have some example code which may be useful but it is too long to share directly on here. I have put it here:

http://tny.cz/c681c8c9

I encountered a similar problem previously and really found this helpful. Unfortunately I can't find the original link to the site it was shared on. Let me know if you need any explanation of it

UPDATE

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

Then you can access the contents of the array in another area

Upvotes: 0

Kirk Powell
Kirk Powell

Reputation: 910

If you want to compound your order results into an array, you have to assign the variable a value before assigning it to the array:

$Polish_Dinner = $_POST['Pol_Desser'];

Or, just assign the array index the appropriate value:

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

To carry the variable across multiple submissions, assign as a session variable.

IF, you can't use a SESSION Variable, then you could use $_GET, but this is not recommended as good practice.

Upvotes: 1

Related Questions