gpt4 book ai didi

javascript - 使用表单将变量从 JavaScript 发布到 PHP

转载 作者:行者123 更新时间:2023-11-30 14:52:01 26 4
gpt4 key购买 nike

我对脚本和编程还很陌生,此时我需要一些帮助。

我有一个带有一些下拉框的表单,用于选择多个选项。我写了一个 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="">
&euro; <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。

最佳答案

您正在设置 innerHTML 属性。请改用 value 来更改输入元素的值。

document.getElementById('postPrice').value = formattedTotalPrice;

虽然 innerHTML 可用于更改页面的 HTML 内容,但从形式。

关于javascript - 使用表单将变量从 JavaScript 发布到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47969763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com