我想用一个表单中的隐藏输入将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>
$salarieid = $_POST['hidden1'];
$query = "select * from salarie where salarieid = ".$salarieid;
echo $query;
$result = mysql_query($query);
<table>
Code for displaying the query result.
</table>
你不能从当前页面的JavaScript代码中传递变量值给当前页面的PHP代码...PHP代码在服务器端运行,它对客户端发生的事情一无所知。
你需要使用其他机制从HTML表单中向PHP代码传递变量,例如使用GET或POST方法提交表单。
<DOCTYPE html> <title>My Test Form</title> </head> <form method="POST"> <p>Please, choose the salary id to proceed result:</p> <label for="salarieids">SalarieID:</label> $query = "SELECT * FROM salarie"; $result = mysql_query($query); if ($result) : <select id="salarieids" name="salarieid"> while ($row = mysql_fetch_assoc($result)) { echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one) </select> <?php endif ?> <input type="submit" value="Sumbit my choice"/> </form> <?php if isset($_POST['salaried']) : ?> $query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid']; $result = mysql_query($query); if ($result) : <table> while ($row = mysql_fetch_assoc($result)) { echo '<tr>'; echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others echo '</tr>'; </table> <?php endif?> <?php endif ?> </body> </html>