1

I'm trying to create a PHP summary page for when a user submits an HTML form.

image of the HTML form.

The form contains different options for hotel rooms. The HTML contains two radio buttons (single or double room) and checkboxes (if a guest wants to include Friday/Saturday/Sunday dinner).

Each of these options is associated to a different price: a single room is $80, a double is $60, and each meal is $8.

As of now I've succeeded in displaying the chosen options on the PHP form.

Here is an image of an example PHP summary page..

But I'm struggling to calculate and then display the total price as a function of which options the guest has selected.

Is there a good way to do this? Something tells me I might need to use arrays, but I haven't figured out how.

In case you wanted to take a look at the HTML Code:

<form action="ski-trip-summary.php" method="post">
<table id="mainTable">
<tr>
  <td class="mainSection"><b>Housing and Meals:</b><br />
    <table>
      <tr>
        <td><i>Room Choices</i><br />
            <input name="room" type="radio" value="single" /> 
              Single ($80)
            <input name="room" type="radio" value="double" checked="checked" />
              Double ($40) </td>
      </tr>
      <tr>
        <td><i>Meals desired ($8 each):</i><br />
            <table>
              <tr><td>Friday Dinner</td>   
                  <td><input name="fridayDinner" type="checkbox" /></td></tr>
              <tr><td>Saturday Dinner</td> 
                  <td><input name="saturdayDinner" type="checkbox" /></td></tr>
              <tr><td>Sunday Dinner</td>   
                  <td><input name="sundayDinner" type="checkbox" /></td></tr>
            </table></td>
      </tr>
    </table>
  </td></tr>
</table>

and the PHP code:

print 'You have signed up for a ';
if ($_POST['room'] == 'single') {
print 'single room.';
} elseif ($_POST['room'] == 'double') {
print 'double room. <br /> <br />' ;
}

print 'Your meal signups are: <br />';

if (isset($_POST['fridayDinner']) && isset($_POST['saturdayDinner']) && isset ($_POST['sundayDinner'])) {
print '<i>Friday dinner <br />' . 'Saturday dinner <br />' . 'Sunday dinner </i>  <br />';

} elseif (isset($_POST['fridayDinner']) && isset($_POST['saturdayDinner'])) {
print '<i>Saturday dinner <br />' . 'Sunday dinner </i>  <br />';

} elseif (isset($_POST['fridayDinner']) && isset($_POST['sundayDinner'])) {
print '<i>Friday dinner <br />' . 'Sunday dinner </i>  <br />';

} elseif (isset($_POST['saturdayDinner']) && isset($_POST['sundayDinner'])) {
print '<i>Saturday dinner <br />' . 'Sunday dinner </i>  <br />';

} elseif (isset($_POST['saturdayDinner']) && isset($_POST['sundayDinner'])) {
print '<i>Saturday dinner <br />' . 'Sunday dinner </i>  <br />';

} else {
print '<i>No meals </i>  <br /><br />';

}

Thanks so much, and I appreciate any input or suggestions on how to do this better/more elegantly!

Chris
  • 4,672
  • 13
  • 52
  • 93
  • will not solve problem, but your dinner checkbox inputs should have the same name, but different values. and dont forget to use `[]` after the name so it will be an array. `` – kscherrer Nov 24 '17 at 15:43
  • Assign variables to each POST array for integers and then add them up with `+`'s - while assigning one variable for them, and concatenating it with the *"Your total is..."*. – Funk Forty Niner Nov 24 '17 at 15:44

2 Answers2

0

your dinner checkbox inputs should have the same name, but different values. and dont forget to use [] after the name so it will be an array.

<input name="dinner[]" type="checkbox" value="friday" />  
<input name="dinner[]" type="checkbox" value="saturday" />  
<input name="dinner[]" type="checkbox" value="sunday" />

Then in the PHP code you can check how many dinners he selected and calculate total:

$total = 0;
$singleDinnerCost = 8;

if ($_POST['room'] == 'single') {
    $total = 80;
} elseif ($_POST['room'] == 'double') {
    $total = 40;
}


if(isset($_POST['dinner'])){
    $dinnerCount = count($_POST['dinner']); // $_POST['dinner'] is an array
    $total += $dinnerCount * $singleDinnerCost;
}

$echo 'Your total cost is $'.$total;

Note that this code is only to calculate total, not to print all the other strings. You can add that part yourself

kscherrer
  • 5,486
  • 2
  • 19
  • 59
  • I think you left out the very part OP was having trouble with, although I may have misunderstood. – Charles Dec 03 '17 at 20:22
0

It looks like you’re almost there! The tricky part is the checkboxes: a good way of solving this is using an array to store the output of the checkboxes. You can see a similar solution here : PHP Multiple Checkbox Array .

To make the dinner variable be returned as an array, make sure to add []to the name field of the HTML form. You should have something like this:

<input name="dinner[]" type="checkbox" value="Friday" />  
<input name="dinner[]" type="checkbox" value="Saturday" />  
<input name="dinner[]" type="checkbox" value="Sunday" />

Then in your PHP,

$roomtype = $_POST['room']
print 'You have signed up for a ';
if ($roomtype == 'single') {
    $roomcost = 80;
    print 'Single room selected <br />';
} elseif ($roomtype == 'double') {
    $roomcost = 40;
    print 'Double room selected <br />';
}


$room = $_POST['room'];
$dinner = $_POST['dinner'];
$singleDinnerCost = 8;


if(isset($dinner)){
    // this part only makes sense if there are dinners selected. 
    // So we check for that in th line above
    // if no meals are checked, the dict will not exist
    print 'Your meal signups are: <br />';

    foreach ($dinner as $key => $day) { 
        print $day . ' dinner <br />';
    }

} else {
    // if no dinners are selected, the dinner dict will not even exit
    print 'You have not selected any dinners ! <br />'
}


if(isset($dinner)){
    // if there are dinners selected, count them
    $dinnerCount = count($dinner); 
} else {
    // if there are no selected dinners, set the dinner count to 0
    $dinnerCount = 0;
}

// compute total price
$total = $dinnerCount * $singleDinnerCost + $roomprice;

print 'Your total cost is $'.$total;

Hope this helps!

Charles
  • 3,116
  • 2
  • 11
  • 20