【发布时间】:2017-12-25 15:02:04
【问题描述】:
我对脚本和编程非常陌生,此时我需要一些帮助。
我有一个带有一些下拉框的表单,用于选择多个选项。我编写了一个 javascript,它根据下拉框中的选定选项计算总价。总值正确返回,但我很难用表单发布这个值。我尝试过隐藏输入类型,但这也不起作用。帖子转到一个php文件,最后必须在de数据库中插入值。
HTML
<form id="vmPrice" class="form-horizontal" method="post"
action="create_xml.php">
<div class="control-group">
<label class="control-label" for="formattedTotalPrice">Costs:</label>
<div class="controls">
<input type="hidden" name="postPrice" id="postPrice" value="">
€ <span id="formattedTotalPrice" name="formattedTotalPrice" >...</span>
/month <br/>
<span style="font-size: 10px;"> By clicking on Create VM a subscription with
the above costs will be automaticly added to your account </span>
</form>
JavaScript
function calculateTotal()
{
var unformattedTotalPrice = getVcpuPrice() + getOsPrice() + getHddPrice() +
getMemoryPrice() + getHAPrice();
var formattedTotalPrice = unformattedTotalPrice;
document.getElementById('formattedTotalPrice').innerHTML =
formattedTotalPrice;
document.getElementById('postPrice').innerHTML = formattedTotalPrice;
}
formattedTotalPrice 使用 span id 正确显示在屏幕上。现在我想用表格发布这个,但它不起作用。
PHP 代码 (create_xml.php)
$price = (int)$_POST['formattedTotalPrice'];
// or
$price = (int)$_POST['postPrice'];
print $price;
此时打印的值为0。
【问题讨论】:
-
span不会发布到 php 端。它不能那样工作。使用input -
但是 呢?那也不行。
-
这行不通,因为您也通过了 span。
<span id="formattedTotalPrice" name="formattedTotalPrice" >...</span>错误.. 只需使用输入,然后将值传递给value属性。使用span显示给用户不要发送到php -
是否有意将价格转换为 INT?它们总是整数,没有美分等吗?
-
等等等等。你应该在服务器上用 PHP 代码再次计算总价,基于他们正在购买的物品的 id 和数量 - 单价和总价应该用 PHP 计算。否则,有人可能会伪造“总数”并将其发送到您的服务器,谁知道接下来会发生什么,他们会为 1000 美元的商品支付 1 美元。
标签: javascript php html forms