PHP calculation from html form

最后都变了- 提交于 2019-12-06 04:34:37

Your ternary statement is currently only checking if $_SESSION['vip'] is set. The problem is that it is set no matter what the user chooses ("yes" or "no") because in the part where you set the session variable, the conditional statement sets the session variable if $_POST['vip'] isn't empty.

Try checking if it's equal to "Yes" like this:

<b>Total Reservation Costs: </b><br><br> £
<?php
     if (isset($_SESSION['party']) && is_numeric($_SESSION['party'])) {
         $party = (int)$_SESSION['party'];
         $vip = (isset($_SESSION['vip']) && $_SESSION['vip'] === 'Yes') ? 5 : 0;
         echo (($party * 5) + $vip);
     }
?>

There is no need to complicate your code with sessions. Just use the $_POST :

<?php

$options =  array(
    1 => '1 Person (+£5)',
    2 => '2 Person (+£10)',
    3 => '3 Person (+£15)',
    ...
    10 => '1 Person (+£50)',
);
?>
...

<select name="party" id="party">
    <option value="-1">Please select</option>
    <?php
        foreach ($options as $persons => $text) {
            $selected = isset($_POST['party'] && $_POST['party'] == $persons) ? ' selected' : '';
            echo '<option value="'.$persons.'".'.$selected.'>'.$text.'</option>';
        }
    ?>
</select>
...
Yes <input type="radio" value="1" name="vip" id="vip_yes" <?php if (isset($_POST['vip']) && $_POST['vip'] == 1) { echo 'checked' } ?> />
No <input type="radio" value="0" name="vip" id="vip_no" <?php if (isset($_POST['vip']) && $_POST['vip'] == 0) { echo 'checked' } ?> />


...
<?php 
    if (isset($_POST['party']) && is_numeric($_POST['party']) ) {
        $price = $_POST['party'] * 5;
        if (isset($_POST['vip']) && $_POST['vip'] == 1) $price += 5;
    }else{
        //-- no post    
        $price = 0;
    }

    if ($price == 0) {
        echo 'Please select your options..';
    }else{
        echo 'Your price is £' . $price;
    }
?>

you need to redo the calculation and add a variable for the cost of being VIP. something like

if ($vip  =='Yes'){$vip_cost = +5)}else{$vip_cost=0}

then add the cost of each person and add the $vip_cost

<?php
$form = '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<select name="party" id="party" >
    <option value="">--Select People--</option>      
    <option value="1">1 People (+£5)</option>        
    <option value="2">2 People (+£10)</option>       
    <option value="3">3 People (+£15)</option>       
    <option value="4">4 People (+£20)</option>       
    <option value="5">5 People (+£25)</option>       
    <option value="6">6 People (+£30)</option>       
    <option value="7">7 People (+£35)</option>       
    <option value="8">8 People (+£40)</option>       
    <option value="9">9 People (+£45)</option> 
    <option value="10">10People (+£50)</option>
</select><br /><br />

<strong> VIP area* : </strong> <br><br>
Yes (+£5) <input type="radio" name="vip" value="Yes">
No <input type="radio" name="vip" value="No"><br /><br />

<input type="submit" value="Submit" name="submit" />';


if (isset($_POST['submit'])){
if (isset($_POST['party'])){
    $cost =  ($_POST['party'] *5); 
    if ($_POST['vip']  =='Yes'){$vip_cost = 5; $vip_text='with vip';}else{$vip_cost=0; $vip_text='';}
$total = ($cost+$vip_cost);
$display = 'Your reservation for '.$_POST['paty'].' '.$vip_text.' will cost '.$total; 
}
    else{
 $display = 'Please Select How Many People<br />'.$form;}

}else{

$display=$form;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php echo $display;?>
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!