从HTML表单进行PHP计算 [英] PHP calculation from html form

查看:85
本文介绍了从HTML表单进行PHP计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为一家餐厅网站创建了一个预订页面,其中有一个表格可以预订晚餐。它包含一个下拉框,允许用户选择将要参加聚会的人数。它还包含一个单选按钮,允许用户选择是否要坐在VIP区域(是/否)。

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 (Yes/ No).

聚会中的每个人都应该花费需额外支付5英镑的预订费,如果他们想坐在VIP区域,则需要额外支付5英镑。表单验证成功后,用户将被转到确认页面,在该页面上将显示他们输入的预订详细信息。

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

当前,我有一个计算出现在我网站的确认页上,可以正确计算聚会的大小费用,但是我的问题是,即使VIP区域选择为否,它仍然会增加5英镑的费用,就像用户选择了是一样。

Currently I have a calculation present on the confirmation page of my website, which correctly calculates the party size costs, but my issue is that even if VIP area selection is "No", it is still adding on the £5 fee as if the user has selected "Yes".

以下是我的预订页面上的相关html代码:

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

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

有谁知道我如何启用计算功能,仅在以下情况下为VIP座位增加额外的£5:用户选择了是?我是网络语言的新手,因此将不胜感激。谢谢!

Does anyone know how I can enable the calculation to only add the additional £5 for VIP area seating if the user has selected "Yes"? I am new to web languages so any help will be appreciated. Thank you!

推荐答案

您的三元语句当前仅检查 $ _ SESSION ['vip'] 已设置。问题在于,无论用户选择什么(是或否),它都会被设置,因为在设置会话变量的部分中,如果 $ _ POST [,则条件语句会设置会话变量。 'vip'] 不为空。

Your ternary statement is currently only checking if $_SESSION['vip'] is set. The problem is that it is set no matter what the user chooses ("yes" or "no") because in the part where you set the session variable, the conditional statement sets the session variable if $_POST['vip'] isn't empty.

尝试检查是否等于是,例如:

Try checking if it's equal to "Yes" like this:

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

这篇关于从HTML表单进行PHP计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆