谷歌搜索后,我仍然感到困惑...
我试图通过隐藏字段将计数的单选按钮值(Javascript)传递给PHP,稍后将其插入数据库MySQL。
我有一个名为test.html的表格

<html>
<head><script type="text/javascript" src="../js/script.js"></script</head>
<body>
<form name="myform" method="post"
 onSubmit="return validateRadio()" action="">

<table class="tftable" border="1">
<tr><th><div align="center" class="tftable th">Questions</div></th><th colspan="2"><div align="center">Answer</div></th>
</tr>
<tr>
  <td>I like to dance</td>
  <td>
  <label></label>
  <label>
  <input name="bm1" type="radio" value="1" />
  Yes</label>
</td>
<td>
<label></label>
<label>
<input name="bm1" type="radio" value="2" />
No
</label>
</td></table>
<input type="hidden" name="passvalue" id="passvalue" value="">
</form>
</body>
</html>


这是计算单选按钮的script.js ...我要传递的值是answeramount值

function validateRadio()
{
// get all the inputs type
var inputs = myform.elements;
var radios = [];

// find the radio type
for (var i = 0; i < inputs.length; ++i) {
if (inputs[i].type == 'radio') {
radios.push(inputs[i]);
}
}
var countChecked = 0;
for (var j = 0; j < radios.length; j++) {
if (radios[j].checked) {
countChecked++;
}
}
//count number of radio button with value=1
var answeramount = 0;
for (var k = 0; k < radios.length; k++){
if(radios[k].checked && radios[k].value==1){
answeramount++;
}
}
if (countChecked != radios.length / 2){
alert("All questions must be answered.");
return false; // abort submit
} else {
alert("All questions have been answered.");
return true; // proceed to submit
}
}


请告诉我如何将“ answeramount”值传递给php,以后再将其插入数据库。谢谢

编辑:

这是一个jsFiddle

最佳答案

$ passvalue = isset($ _ POST ['passvalue'])?$ _ POST ['passvalue']:“”;

密码字段应该

输入type =“ hidden” name =“ passvalue” id =“ passvalue” value =“ echo $ passvalue;”

而在JavaScript

alert(“所有问题均已回答。”);
document.getElementsById('passvalue')。Value = answeramount;

10-07 19:02