如何将JavaScript变量传递给PHP? [英] How to pass JavaScript variables to PHP?

查看:149
本文介绍了如何将JavaScript变量传递给PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用表单中的隐藏输入将JavaScript变量传递给PHP。

I want to pass JavaScript variables to PHP using a hidden input in a form.

但我无法将 $ _ POST ['hidden1'] 的值转换为 $ salarieid 。有什么不对?

But I can't get the value of $_POST['hidden1'] into $salarieid. Is there something wrong?

以下是代码:

<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>


推荐答案

您无法从当前页面javascript传递变量值当前页面PHP代码... PHP代码在服务器端运行,它不知道客户端发生了什么。

You cannot pass variable values from the current page javascript 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.

你需要使用其他机制将变量从html-form传递给PHP代码,例如在GET或POST方法上提交表单。

You need to pass variables to PHP code from html-form using another mechanism, such as submitting form on GET or POST methods.

<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>

这篇关于如何将JavaScript变量传递给PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆