Reputation: 531
I am in the process of designing a php validated form for a restaurant website which directs the user to a thank you page, displaying their reservation details, once they have pressed submit. Currently I have 2 variables, one named 'party', the other named 'vip'. These variables both display a string value when echoed. However, I wish to assign them an int value and then add them together to display a total price to the user.
Currently I have this code which does not seem to function:
<b>Total Reservation Costs: </b> £
<?php
if ( $party == "1 Person (+£5)" )
{
$party = 5;
}
elseif if ( $party == "2 People (+£10)" )
{
$party = 10;
}
elseif if ( $party == "3 People (+£15)" )
{
$party = 15;
}
elseif if ( $party == "4 People (+£20)" )
{
$party = 20;
}
elseif if ( $party == "5 People (+£25)" )
{
$party = 25;
}
elseif if ( $party == "6 People (+£30)" )
{
$party = 30;
}
elseif if ( $party == "7 People (+£35)" )
{
$party = 35;
}
elseif if ( $party == "8 People (+£40)" )
{
$party = 40;
}
elseif if ( $party == "9 People (+£45)" )
{
$party = 45;
}
elseif if ( $party == "10+ People (+£50)" )
{
$party = 50;
}
if ( $vip == "Yes" )
{
$vip = 5;
}
elseif if ( $vip == "No" )
{
$vip = 0;
}
echo $party + $vip;
?>
If anyone knows how I can get this to work I would be very grateful, I am new to web languages so I apologise in advance if my answer isn't very clear. Thank you.
Upvotes: 0
Views: 9350
Reputation: 16506
Instead of using a string to determine the number of people why not just use an int
Here is a complete program
<form action="" method="POST">
<select name="party">
<option value="1">1 People (+£5)</option>
<option value="2">2 People (+£10)</option>
<!-- ... -->
<option value="10">10+ People (+£50)</option>
</select>
<input type="checkbox" name="vip" />
<input type="submit" value="Confirm Reservation" />
</form>
<?php
if (isset($_POST['party']) && is_numeric($_POST['party'])) {
$party = (int)$_POST['party'];
$vip = isset($_POST['vip']) ? 5 : 0;
echo "Total is: " . (($party * 5) + $vip);
}
and elseif if
should be just else if
Upvotes: 2
Reputation: 577
It's not elseif if, its elseif
Total Reservation Costs: £
<?php
if ( $party == "1 Person (+£5)" ){
$party = 5;
}
elseif( $party == "2 People (+£10)" ){
$party = 10;
}
elseif( $party == "3 People (+£15)" ){
$party = 15;
}
elseif( $party == "4 People (+£20)" ){
$party = 20;
}
elseif( $party == "5 People (+£25)" ){
$party = 25;
}
elseif ( $party == "6 People (+£30)" ){
$party = 30;
}
elseif( $party == "7 People (+£35)" ){
$party = 35;
}
elseif( $party == "8 People (+£40)" ){
$party = 40;
}
elseif( $party == "9 People (+£45)" ){
$party = 45;
}
elseif( $party == "10+ People (+£50)" ){
$party = 50;
}
else{ $party = 0;
}
if ( $vip == "Yes" ){
$vip = 5;
}
elseif ( $vip == "No" ){
$vip = 0;
}
else{
$vip = 0;
}
echo $party + $vip;
?>
`
I'd suggest adding an else statement too, as i have added in my answer, to handle any other cases that might occur.
Upvotes: 0