【发布时间】:2017-04-29 02:02:00
【问题描述】:
我想使用表单中的隐藏输入将 JavaScript 变量传递给 PHP。
但我无法将$_POST['hidden1'] 的值转换为$salarieid。有什么问题吗?
代码如下:
<script type="text/javascript">
// View what the user has chosen
function func_load3(name) {
var oForm = document.forms["myform"];
var oSelectBox = oForm.select3;
var iChoice = oSelectBox.selectedIndex;
//alert("You have chosen: " + oSelectBox.options[iChoice].text);
//document.write(oSelectBox.options[iChoice].text);
var sa = oSelectBox.options[iChoice].text;
document.getElementById("hidden1").value = sa;
}
</script>
<form name="myform" action="<?php echo $_SERVER['$PHP_SELF']; ?>" method="POST">
<input type="hidden" name="hidden1" id="hidden1" />
</form>
<?php
$salarieid = $_POST['hidden1'];
$query = "select * from salarie where salarieid = ".$salarieid;
echo $query;
$result = mysql_query($query);
?>
<table>
Code for displaying the query result.
</table>
【问题讨论】:
-
您可以将 js 变量值存储在 cookie 中,然后在 php cookie 中访问该变量。
-
@shasikanth - 你可以这样做,但是cookie直到第二次查看页面时才会设置。那时,它将是来自 first 页面视图的值 - 它始终是落后的。 cookie 不是一种动态 将信息传回服务器的机制;您必须执行其他操作才能刷新页面,例如POST 表单,或进行 Ajax 调用。如果您正在执行其中之一,则没有理由使用 cookie - 只需在您使用的机制中传递信息。此外,页面消失后 cookie 将持续存在 - 这不是这个问题的意图。
标签: php javascript variables