0

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.

1
  • ummm elseif if I guess it works but it's not what you mean to write. Commented Nov 20, 2014 at 22:32

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Because as the user is displayed their reservation details I wish for them to be told on page "Party size: 4 people (+£20)". I then wish to take this 20 from their selection and add it to the 5 if they have selected VIP area seating. Although, if you have a simplified method to make this work, please let me know
@JamesPatterson please see the updated answer for a nice way of implementing your form and form substitution script.
I applied your suggestions however I still am not presented with a total cost :( Although, no errors are thrown up either. Why might this be happening?
@JamesPatterson Yeah, is_int should have been is_numeric. Give it a go now.
Still the same result, also I notice that after the "Total Reservation Costs: " line, the page is cut off? :(
|
0

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.

4 Comments

Hi friend, I tried your suggestion however I just got no such variable errors on screen once the thank you page loads. Why might this be happening? :(
I get an "Notice: Undefined variable: party in" error on each line the $party is mentioned in your code and also an "Notice: Undefined variable: vip" error where $vip is mentioned :(
did you initialize thos variables as per your code $party = $_POST['party']; $vip = isset($_POST['vip']) ? 5 : 0; ?
Sorry I am really new to weblanguages, can you explain what you mean by this? For help, here is my reservations page in its entirety: pastebin.com/UNDkAEYT - here is also my thank you page in its entirety: pastebin.com/31pdBnD5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.