Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

php - 如何将JavaScript变量传递给PHP?(How do I pass JavaScript variables to PHP?)

I want to pass JavaScript variables to PHP using a hidden input in a form.(我想使用表单中的隐藏输入将JavaScript变量传递给PHP。)

But I can't get the value of $_POST['hidden1'] into $salarieid .(但是我无法将$_POST['hidden1']放入$salarieid 。) Is there something wrong?(有什么不对?) Here is the code:(这是代码:) <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>   ask by Dylan SUN translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot pass variable values from the current page JavaScript code to the current page PHP code... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.(您不能将变量值从当前页面的JavaScript代码传递到当前页面的PHP代码... PHP代码在服务器端运行,并且对客户端的运行情况一无所知。)

You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.(您需要使用其他机制(例如,使用GET或POST方法提交表单)将变量从HTML表单传递给PHP代码。) <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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...