简体   繁体   English

将值从javascript传递到PHP(使用隐藏字段)

[英]Passing value from javascript to PHP ( Using Hidden Field )

After doing some googling, I still get confused... I'm trying to pass the counted radio button value (Javascript) to PHP through hidden field that later on will be inserted to database MySQL. 经过一番谷歌搜索后,我仍然感到困惑...我试图通过隐藏字段将计数的单选按钮值(Javascript)传递给PHP,之后将其插入到数据库MySQL中。 I have a form named test.html 我有一个名为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>

Here's script.js that counted the radio button... The value that I want to pass is answeramount value 这是计算单选按钮的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
} 
}

Please show me how to pass "answeramount" value to php that later on will be inserted to database. 请告诉我如何将“ answeramount”值传递给php,稍后将其插入数据库。 Thanks 谢谢

EDIT: 编辑:

Here is a jsFiddle 这是一个jsFiddle

You can use that simple code to manipulate a input's value: 您可以使用该简单代码来操纵输入的值:

var field = document.getElementById('#passvalue');
field.value = answeramount;

Place this snipped right between your counting and the alert, 将此片段放在您的计数和警报之间,

The php side would be a standard POST to sql script (there are several tutorials out there) Your hidden input would be $_POST['passvalue'] php端将是标准的POST到sql脚本(那里有一些教程),您的隐藏输入将是$ _POST ['passvalue']

correct your HTML: read the comments! 更正您的HTML:阅读评论!

and in your function add 并在您的功能中添加

...
else {
    alert("All questions have been answered.");
    document.getElementById("passvalue").value = answeramount;
    return true; // proceed to submit
}
...

In PHP you can get the value from $_POST[ 'passvalue' ] . 在PHP中,您可以从$ _POST ['passvalue']获取值。

<?php
$passvalue = ( isset( $_POST[ 'passvalue' ] ) ? (int)$_POST[ 'passvalue' ] : 0  );
?>

update: better readable code: 更新:可读性更好的代码:

<?php
// check: if the index "passavlue" is set
// equivalent to ternary operator [ expression ? <give true> : <give false> ] 
if( isset( $_POST[ 'passvalue' ] ) ){
    // cast the string to integer
    $passvalue = (int) $_POST[ 'passvalue' ];
}
else{
    $passvalue = 0;
}
?>

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

The passvalue field should 密码字段应该

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

And at the javascript 而在JavaScript

alert("All questions have been answered."); alert(“所有问题均已回答。”); document.getElementsById('passvalue').Value = answeramount; document.getElementsById('passvalue')。Value = answeramount;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM