我想使用表单中的隐藏输入将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>

推荐答案

您不能将变量值从当前页面JavaScript代码传递到当前页面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>

Php相关问答推荐

为什么这个方法调用用引号和花括号包裹?

添加自定义Metabox到WooCommerce管理订单,启用HPOS

在WooCommerce中执行一次银行电汇确认付款代码

PHP DOMDocument忽略第一个表S结束标记

一列必须指定多个变量的Laravel查询

如何显示每个分类的当前术语的帖子中包含的前20个标签,不包括标签分类

WooCommerce:在 checkout 审核单和发票中显示定制产品数据

PHP-带POST验证的for循环

WooCommerce在购买后多次重定向

将SVG包含在另一个php生成的SVG中

PHP -将字符串拆分为两个相等的部分,但第二个字符串中的单词更多

Laravel处理belongToMany和额外字段

过滤会员计划标题和标签

Google Sheets PHP API - 添加多个选项卡时出现问题

Woocommerce的数量和价格条件

在WooCommerce可变产品中显示所选变体的自定义字段

在WooCommerce中显示 checkout 的高级自定义字段(Advanced Custom Fields)并保存其值

PHP:如何将多列sql查询结果转换成数组

password_verify 在 PHP 7.4 中有效,但在 PHP 8.2 中无效

如何在不复制的情况下在php中重新索引数组