[英]Save option form and pass JS variable
I have a form that has radiobuttons and listboxes. 我有一个带有单选按钮和列表框的表单。 I am trying to call a php file on button press to save the selected options into a MySQL database.
我试图在按下按钮时调用一个php文件,以将选定的选项保存到MySQL数据库中。 I would like to pass the selected options as well as the total_cost, which is a JS variable.
我想传递选定的选项以及total_cost,这是一个JS变量。
How can I pass the JS variable to the php file when the form submit button is pressed? 当按下表单提交按钮时,如何将JS变量传递给php文件?
<form action="saveConfig.php" class="hide-submit" method="post">
<div class="btn-configure step2button">
<span class="pcButtonText">
Save
</span>
</div>
<ul id="radio" class="input-list">
<li>
<input id="item-1" name="config-prod" value="1.00" type="radio" onchange="updateTotal();">
<label for="item-1">Item 1 [£1.00]</label>
</li>
<li>
<input id="item-2" name="config-prod" value="12.34" type="radio" onchange="updateTotal();">
<label for="item-2">Item 2 [£12.34]</label>
</li>
<li>
<input id="item-3" name="config-prod" value="9.99" type="radio" onchange="updateTotal();">
<label for="item-3">Item 3 [£9.99]</label>
</li>
</ul>
<select id="plist" name="partlist" onchange="updateTotal();">
<option value="99.99">CPU 1</option>
<option value="123.00">CPU 2</option>
<option value="250.54">CPU 3</option>
</select>
</form>
<br>
Total: <input id="total" type="text">
JS: JS:
let buildcost = 25.00;
function updateTotal() {
let radios = document.getElementsByName('config-prod');
let select = document.getElementById('plist');
let partcost = 0;
for (let i = 0, j = radios.length; i < j; i++) {
if (radios[i].checked) {
partcost = parseFloat(radios[i].value);
break;
}
}
partcost += parseFloat(select.options[select.selectedIndex].value);
let total_cost = buildcost + partcost;
document.getElementById('total').value = total_cost.toFixed(2);
}
I cannot figure out how to pass the variable total_cost. 我无法弄清楚如何传递变量total_cost。 Any advice would be appreciated.
任何意见,将不胜感激。
you will have to create an input element then append that element to the form and then submit the form. 您将必须创建一个input元素,然后将该元素添加到表单中,然后提交表单。
var inputElement=document.createElement("input");
inputElement.setAttribute("value",total_cost);
var form= document.getElementById("formId");
form.appendChild(inputElement);
Use this to save your changes, then in saveConfig.php recive your data with json_decode($_POST) 使用它来保存您的更改,然后在saveConfig.php中使用json_decode($ _ POST)接收数据
<form action="#" class="hide-submit">
<div class="btn-configure step2button">
<span class="pcButtonText">
Save
</span>
</div>
<ul id="radio" class="input-list">
<li>
<input id="item-1" name="config-prod" value="1.00" type="radio" onchange="updateTotal();">
<label for="item-1">Item 1 [£1.00]</label>
</li>
<li>
<input id="item-2" name="config-prod" value="12.34" type="radio" onchange="updateTotal();">
<label for="item-2">Item 2 [£12.34]</label>
</li>
<li>
<input id="item-3" name="config-prod" value="9.99" type="radio" onchange="updateTotal();">
<label for="item-3">Item 3 [£9.99]</label>
</li>
</ul>
<select id="plist" name="partlist" onchange="save();">
<option value="99.99">CPU 1</option>
<option value="123.00">CPU 2</option>
<option value="250.54">CPU 3</option>
</select>
</form>
<br>
Total: <input id="total" type="text">
<script>
function save(){
$.ajax({
url : 'saveConfig.php',
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data)
{
alert('saved');
},
error: function (jqXHR, textStatus, errorThrown){
alert(errorThrown);
}
});
}
</script>
No 1 : you have kept Total: <input id="total" type="text">
out from closing </form>
tag. 否1:您已将Total:
<input id="total" type="text">
保留在</form>
标记之外。 keep it inside the </form>
tag. 将其保留在
</form>
标记内。
No2 : Input field <input id="total" type="text">
is missing name attribute. No2:输入字段
<input id="total" type="text">
缺少名称属性。 it should be like <input id="total" type="text" name="total">
它应该像
<input id="total" type="text" name="total">
Hope this will work 希望这会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.