0

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.

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.

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.

I wish to add these two factors together and display a total booking fee to the user on the confirmation page, but the code I have currently, is not performing this.

Here is the relevant 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 code on my confirmation page:

<b>Total Reservation Costs: </b> £
 <?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);
}
?>

Currently no calculation is echoed, would anyone be able to correct my code so I can understand what I did wrong? Thank you

8
  • Is "Total is:" echoed? Commented Nov 21, 2014 at 18:35
  • @dan08 No, it is not Commented Nov 21, 2014 at 18:43
  • I thought my answer was not helpful? stackoverflow.com/questions/27050838/adding-two-variables-php/… Commented Nov 23, 2014 at 0:13
  • @robbmj clearly you can't read. I thanked you for your help, if you recall I wasn't impressed with your rudeness. Commented Nov 23, 2014 at 1:53
  • Your quote "your answer has not provided with me a solution to my problem". Clearly it answered your question as you have copied it into this one and this one stackoverflow.com/questions/27078470/…. Note how your original question (stackoverflow.com/questions/27050838/adding-two-variables-php) made no mention of sessions. S.O. is not a place to have people write your entire application for you. A point I tried to make before when you sent me links to pastebin and asked me to fit my answer into the rest of your code. Commented Nov 23, 2014 at 2:18

2 Answers 2

3

You have a couple problems that may be contributing to the calculation being echoed.

First, change

<select name="party" id="party" value="<?php echo $party;?>">

to

<select name="party" id="party">

since your value is coming from the option list.

Next, change:

(isset($party) && $party=="1")

to

(isset($_SESSION['party']) && $_SESSION['party']=="1")

Third, you way want to harden your post parameters .. something like this maybe:

$_SESSION['party'] = htmlentities($_POST['party'],ENT_QUOTES);
Sign up to request clarification or add additional context in comments.

Comments

0

I think we'd need to see the "user is sent to the confirmation page" portion of the application, but if you validate and then redirect user to a new page, the $_POST global will be empty. It appears you're pushing POST into SESSION, so maybe something like this instead:

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

Also, just as an aside - you might not want to shove unvalidated $POST information into $SESSION for your application's security. Validate the $POST information, then push it into $SESSION

9 Comments

This seems to be working however the total cost echoed isnt correct, maybe theres an mistake in your calculation somewhere? :( Eg. if i put 1 person (£5) and yes to vip (£5), £30 total cost is displayed
Actually oddly enough it seems to be working now? not sure why I first got an incorrect price
Cool, glad you got it working! The difference you're seeing may be because of data that was inside of SESSION before-hand, since the session is PHP's attempt at being stateful. Also please give some though to what @AdamMacDonald had to say regarding hardening your post parameters. Never trust user entered data!
Just noticed it still adds 5 to the vip even if it has been checked as no, is there a way I can fix this? Thanks so much for all of your feedback, I really appreciate it. I will take into consideration what you have told me
No problem. So check out the function isset() on php.net when you get a chance - knowing how isset vs. empty vs. is_null etc. will be something that you'll have to deal with all the time. isset() will return true if the variable is set AND isn't null - so if it's empty, it will still resolve as true. Try (isset($_SESSION['vip'] ) && $_SESSION['vip'] == 1)) in your ternary operator.
|

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.