I have created a reservations page for a restaurant website, with a form present to book a dinner reservation. It contains a dropdown box which allows the user to select the amount of people who will be dining in their party. It also contains a radio button to allow the user to select if they wish to be seated in the VIP area (Yes/ No).
Each person in the party should cost an extra £5 towards their booking fee and if they wish to be seated in the VIP area, they will be charged an additional £5. Once the form validation is successful, the user will be sent to a confirmation page, where the reservation details they have entered, will be displayed to them.
Currently I have a calculation present on the confirmation page of my website, which correctly calculates the party size costs, but my issue is that even if VIP area selection is "No", it is still adding on the £5 fee as if the user has selected "Yes".
Here is the relevant html code on my reservations page:
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['party'] = $_POST['party'];
}
if ( !empty($_POST['vip']))
$_SESSION['vip'] = $_POST['vip'];
?>
...
<strong>Select Party Size* :</strong>
<br>
<select name="party" id="party" value="<?php echo $party;?>">
<option value="">Please Select</option>
<option <?php if (isset($party) && $party=="1") echo "selected";?> value="1">1 Person (+£5)</option>
<option <?php if (isset($party) && $party=="2") echo "selected";?> value="2">2 People (+£10)</option>
<option <?php if (isset($party) && $party=="3") echo "selected";?> value="3">3 People (+£15)</option>
<option <?php if (isset($party) && $party=="4") echo "selected";?> value="4">4 People (+£20)</option>
<option <?php if (isset($party) && $party=="5") echo "selected";?> value="5">5 People (+£25)</option>
<option <?php if (isset($party) && $party=="6") echo "selected";?> value="6">6 People (+£30)</option>
<option <?php if (isset($party) && $party=="7") echo "selected";?> value="7">7 People (+£35)</option>
<option <?php if (isset($party) && $party=="8") echo "selected";?> value="8">8 People (+£40)</option>
<option <?php if (isset($party) && $party=="9") echo "selected";?> value="9">9 People (+£45)</option>
<option <?php if (isset($party) && $party=="10") echo "selected";?> value="10">10+ People (+£50)</option>
</select>
<strong> VIP area* : </strong> <br><br>
Yes (+£5) <input type="radio" name="vip" <?php if (isset($vip) && $vip=="Yes") echo "checked";?> value="Yes">
<br><span id="vip" class="error"><?php echo $vipErr;?></span><br>
No <input type="radio" name="vip" <?php if (isset($vip) && $vip=="No") echo "checked";?> value="No">
Here is the relevant calculation on the confirmation page:
<b>Total Reservation Costs: </b><br><br> £
<?php
if (isset($_SESSION['party']) && is_numeric($_SESSION['party'])) {
$party = (int)$_SESSION['party'];
$vip = isset($_SESSION['vip']) ? 5 : 0;
echo (($party * 5) + $vip);
}
?>
Does anyone know how I can enable the calculation to only add the additional £5 for VIP area seating if the user has selected "Yes"? I am new to web languages so any help will be appreciated. Thank you!
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>
来源:https://stackoverflow.com/questions/27078470/php-calculation-from-html-form