第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

插入自定義 HTML 字段值并在 PHP 中訪問(wèn)它

插入自定義 HTML 字段值并在 PHP 中訪問(wèn)它

躍然一笑 2021-09-04 17:41:01
我正在嘗試插入用于計(jì)算需要存儲(chǔ)在數(shù)據(jù)庫(kù)中的總價(jià)格的 javascript,但是在我提交表單后,該字段在數(shù)據(jù)庫(kù)中為空白。我需要在網(wǎng)站上顯示并插入到數(shù)據(jù)庫(kù)中的總成本。我試圖把兩種形式的價(jià)格。一種是顯示javascript。另一種是將結(jié)果插入到數(shù)據(jù)庫(kù)中。我想我應(yīng)該編輯 HTML 代碼來(lái)解決這個(gè)問(wèn)題,但我不知道如何。請(qǐng)幫忙這是 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')";這是數(shù)據(jù)庫(kù):+----+------------+------------------------+------------+---------+-----------+----------+-------------------+---------------+------------+------------------------+----------------+-----------+| 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         |我可以在網(wǎng)站上顯示總成本,但我無(wú)法將總成本插入到數(shù)據(jù)庫(kù)中。標(biāo)簽表中的所有其他內(nèi)容都已成功插入到數(shù)據(jù)庫(kù)中。如何將 totalCost javascript 插入到數(shù)據(jù)庫(kù)中。
查看完整描述

1 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

向我們展示整個(gè)<form>代碼會(huì)很有幫助,但對(duì)我來(lái)說(shuō),問(wèn)題似乎在于您沒有在隱藏輸入字段中設(shè)置總價(jià)的值。

在您的 JS 中,您通過(guò)分配來(lái)顯示總價(jià),totalCostEl.textContenttotalCostEl僅指不隨表單一起發(fā)送的 span 元素。您需要做的是選擇隱藏的輸入元素并將總價(jià)格值也放在那里,以便將其發(fā)送到服務(wù)器。也可能更容易為該隱藏字段提供一個(gè) id,例如id="totalPriceInput"這樣您就可以在您的 JS 中輕松引用它。

所以,

<input type="hidden" name="totalCost">

需要是

<input type="hidden" name="totalCost" id="totalPriceInput">

在你的 jS 中,你可以做類似的事情

document.getElementById('totalPriceInput').value = "the calculated total price here"

提交表單時(shí),只有從用戶那里獲取輸入的某些元素才會(huì)被發(fā)送到服務(wù)器。


查看完整回答
反對(duì) 回復(fù) 2021-09-04
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)