douci6541 2014-11-14 13:33
浏览 22
已采纳

执行服务器端,简单的计算

I am new to php and have been trying to enable the website I am working on, to correctly calculate the price of two factors on a reservation page for a restaurant (party size & VIP area selection). Currently I have implemented javascript calculations so that on the reservation page a total price is displayed automatically once the user makes their selections, however I wish for the confirmation php page which is displayed once the reservations form has been submitted, to perform the calculations on the server side.

Here is the html code in question (reservations.html):

<strong>Select Party Size:</strong>
<br>
<select name="party" id="party" onblur="validateSelect(name)">
<option value="">Please Select</option>
<option value="5 (+£5)">1 Person (£5)</option>
<option value="10 (+£10)">2 People (£10)</option>
<option value="15 (+£15)">3 People (£15)</option>
<option value="20 (+£20)">4 People (£20)</option>
<option value="25 (+£25)">5 People (£25)</option>
<option value="30 (+£30)">6 People (£30)</option>
<option value="35 (+£35)">7 People (£35)</option>
<option value="40 (+£40)">8 People (£40)</option>
<option value="45 (+£45)">9 People (£45)</option>
<option value="50 (+£50)">10+ People (£50)</option>
</select>

...

<strong> VIP Area? </strong>
<br>
Yes (+£5) <input name="hand" id="left" value="5 (+£5)" onblur="validateRadio(id)" type="radio">
No <input name="hand" id="right" value="0" onblur="validateRadio(id)" checked="" type="radio">
<span class="validateError" id="handError" style="display: none;">Please specify whether you would like a table in the VIP area.</span>

Here is the php code I currently have which is not fully functioning (confirmation.php):

<b>Total Reservation Costs: </b>
<?php $party + $hand

If anyone knows how I can correct my php code to perform the calculations of the party and VIP area selections and display this on the confirmation php page, I would be appreciative for any suggestions.

Thank you

  • 写回答

2条回答 默认 最新

  • doujiyun0041 2014-11-14 13:52
    关注

    When you're working with the form elements select, checkbox, radio one thing to remember is that the value attribute is not visible to the user, so you can set these how you like.

    With that in mind, lets look at your select options

    <option value="5 (+£5)">1 Person (£5)</option>
    <option value="10 (+£10)">2 People (£10)</option>
    <option value="15 (+£15)">3 People (£15)</option>
    

    There's no need to add the (+£5) in the value, simply supply the number, this will make calculations much easier for you.

    That will mean it looks like this

    <option value="5">1 Person (£5)</option>
    <option value="10">2 People (£10)</option>
    <option value="15">3 People (£15)</option>
    

    We will do the same for your radio button:

    <input name="hand" id="left" value="5 (+£5)" onblur="validateRadio(id)" type="radio">
    

    turns into

    <input name="hand" id="left" value="5" onblur="validateRadio(id)" type="radio">
    

    Now, on the page where your form is submitted to, you can get the values like this

    $party = intval( $_POST['party'] );
    $hand = intval( $_POST['hand'] );
    $cost = $party + $hand;
    
    ?>
    <b>Total Reservation Costs: </b> <?php echo $cost; ?>
    

    I am assuming you're using method="post" in your HTML form, if you are using method="get" then you need to change $_POST to $_GET

    You'll notice that I am using intval(), the reason for doing this is to convert the string into an int value, this may be unnecessary but I am using it to demonstrate how to convert string to int, intval() only works for whole numbers more information on intval()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?