PHP:根据用户从HTML表单中选择的选项计算总价

时间:2017-11-24 15:39:34

标签: php html forms

我正在尝试为用户提交HTML表单时创建PHP摘要页面。

image of the HTML form

表单包含酒店客房的不同选项。 HTML包含两个收音机按钮(单人或双人房间)和复选框(如果客人希望包含周五/周六/周日晚餐)。

这些选项中的每一个都与不同的价格相关:单个房间是80美元,双人房是60美元,每餐是8美元。

截至目前,我已成功在PHP表单上显示所选选项。

Here is an image of an example PHP summary page.

但我正在努力计算,然后显示总价作为客人选择的选项的函数。

这样做有好办法吗?有些东西告诉我,我可能需要使用数组,但我还没弄明白怎么做。

如果您想查看 HTML代码

<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>

PHP代码

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 />';

}

非常感谢,我感谢任何有关如何做得更好/更优雅的意见或建议!

2 个答案:

答案 0 :(得分:0)

您的晚餐复选框输入应具有相同的名称,但值不同。并且不要忘记在名称后面使用[],这样它就是一个数组。

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

然后在PHP代码中,您可以查看他选择的晚餐数量并计算总数:

$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;

请注意,此代码仅用于计算总数,而不是用于打印所有其他字符串。您可以自己添加该部分

答案 1 :(得分:0)

看起来你快到了!棘手的部分是复选框:解决这个问题的一个好方法是使用数组来存储复选框的输出。您可以在此处看到类似的解决方案:PHP Multiple Checkbox Array

要使晚餐变量作为数组返回,请务必将[]添加到HTML表单的name字段中。你应该有这样的东西:

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

然后在你的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;

希望这有帮助!