发布日期:2018-03-26
如何将JavaScript变量传递给PHP?+ 查看更多
如何将JavaScript变量传递给PHP?
+ 查看更多
发布日期:2018-03-10 16:23
分类:PHP
浏览次数:113
我想利用表单中的hidden类型input标签将JavaScript变量传递给PHP。
但我无法将$_POST['hidden1'] 的值传递给 $salarieid,请问什么地方做错了?这是我的代码
但我无法将$_POST['hidden1'] 的值传递给 $salarieid,请问什么地方做错了?这是我的代码
<script type="text/javascript"> // view which the user has chosen function func_load3(name){ var oForm = document.forms["myform"]; var oSelectBox = oForm.select3; var iChoice = oSelectBox.selectedIndex; //alert("you have choosen: " + 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 display the query result. </table>
提前先感谢你们的建议
回答
你当然无法将变量从当前页面传递给当前页面的PHP代码,PHP代码运行在服务器端,它不知道客户那边在做什么。你应该用另一种机制来吧html表单里的变量传递给PHP代码,比如使用GET或者POST方式提交表单。<DOCTYPE html> <html> <head> <title>My Test Form</title> </head> <body> <form method="POST"> <p>Please, choose the salary id to proceed result:</p> <p> <label for="salarieids">SalarieID:</label> <?php $query = "SELECT * FROM salarie"; $result = mysql_query($query); if ($result) : ?> <select id="salarieids" name="salarieid"> <?php 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 ?> </p> <p> <input type="submit" value="Sumbit my choice"/> </p> </form> <?php if isset($_POST['salaried']) : ?> <?php $query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid']; $result = mysql_query($query); if ($result) : ?> <table> <?php 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>