简体   繁体   English

PHP:向表单添加价格

[英]PHP: adding prices to a form

I am trying to edit my PHP script so the prices I have added to the form will be added as a total when the user clicks submit but I am not sure how to add up the values. 我正在尝试编辑PHP脚本,因此当用户单击“提交”时,我添加到表单中的价格将作为总计添加,但是我不确定如何将这些值相加。

HTML 的HTML

<select name="service">
<option value="Yes" value="10">Yes £10</option>
<option value="No">No</option>
</select>

<form method="post" action="example.php">
<select name="booking">
<option value="Double Room" value="50">Double Room £50</option>
<option value="Twin Room" value="70">Twin Room £70</option>
<option value="Family Room" value="100">Family room £100</option>
</select>
<br /> <br />
<input type="submit" value="submit" action="example.php" name="submit"><br />
</form>

PHP 的PHP

You have choosen the <?php echo $_POST["booking"]; ?>

You're defined twice the value attribute of the options tags and this is an error . 您为options标签的value属性定义了两次,这是一个错误

If you want to access the price, just set the price as a single value attribute. 如果要访问价格,只需将价格设置为单个value属性。

A better way would be to identify each row with an ID and having a corresponding array in PHP to get both the name and the price (either as an array again, or better as an object). 更好的方法是用ID标识每一行,并在PHP中具有相应的数组以获取名称和价格(可以再次作为数组,也可以作为对象)。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM