我正在尝试插入用于计算需要存储在数据库中的总价格的 javascript,但在我提交表单后,该字段在数据库中为空白。我需要将总成本显示在网站上并插入到数据库中。
我试图把两种形式的价格。一种是显示javascript。另一种是将结果插入到数据库中。我想我应该编辑 HTML 代码来解决这个问题,但我不知道如何。请帮忙
这是 HTML 代码:
Price: <span id="totalCost"></span><input type="hidden" name="totalCost">
这是一些Javascript:
var totalCostEl = document.querySelector('#totalCost');
function calculateTotal() {
var unitCost = product_price[productEl.value];
var additionalCost = size_price[sizeEl.value] || 0;
var qty = quantityEl.value || 0;
totalCostEl.textContent = `Total cost: $${(unitCost + additionalCost) * qty}`;
}
这是php:
if (!$con)
{die("Failed to connect to MySQL: " . mysqli_connect_error()); }
else
{ echo "Order Succeed!" ;}
$var_Price = $_POST["totalCost"];
$sql="INSERT INTO label (product, quantity, size, base, scents, ingredients, packaging, name, email, phone, message, totalCost)
VALUES('$var_Product','$var_Quantity','$var_Size','$var_Base','$var_Scent','$var_Ingredients','$var_Packaging','$var_Name','$var_Email','$var_Phone','$var_Message','$var_Price')";
这是数据库:
+----+------------+------------------------+------------+---------+-----------+----------+-------------------+---------------+------------+------------------------+----------------+-----------+
| id | name | email | phone | message | product | quantity | size | base | scents | ingredients | packaging | totalCost |
+----+------------+------------------------+------------+---------+-----------+----------+-------------------+---------------+------------+------------------------+----------------+-----------+
| 22 | xxx | xxx | xxx | None | Face Soap | 1 | Regular Size(3oz) | Dry Skin | Clove | Shea Butter | Co-Brand Label | 0 |
我可以在网站上显示总成本,但我无法将总成本插入到数据库中。标签表中的所有其他内容都已成功插入到数据库中。如何将 totalCost javascript 插入到数据库中。
动漫人物