【问题标题】:Unable to get $_GET values from form无法从表单中获取 $_GET 值
【发布时间】:2019-05-31 16:01:55
【问题描述】:

我无法从表单中检索 $_GET['id']$_GET['action'] 并在此页面中输入:

<?php
include "common.php";
var_dump($_GET['quantity']);
var_dump($_GET['action']);
var_dump($_GET['id']);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>es6.1.6</title>
    <style type="text/css">
        input {
            margin: 10px;
        }
    </style>
</head>
<body>
<ul>
    <?php
    for($i=1; $i<=sizeof($names); $i++) {
        echo "<form method='get' action='shop.php?action=add&id=$i'>";
        echo "<li><a href='description.php?item=$i'> ",$names[$i],"</a></li>";
        echo "<input type='number' name='quantity'>";
        echo "</form>";
    }
    ?>
</ul>

<br><br>
<form action="summary.php">
    <input type="submit" value="Add to cart">
</form>

</body>
</html>

只有 $_GET['quantity'] 会在插入数字并按 Enter 键进入任何输入表单后设置

【问题讨论】:

  • @NigelRen 问题是未设置 action=add 以及 id=$i 我已编辑帖子
  • 这两种形式有不同的action...第一种shop.php?action=add&amp;id=$i,而第二种summary.php不可能同时按预期工作,因为第一个加载shop.php而第二个加载@ 987654329@
  • 我认为只会执行带有action="summary.php" 的第二个表单,因为它是唯一具有&lt;input type="submit"... 的表单,因为没有submit 输入类型,所以不会执行第一个表单...所以你永远不会得到 actionid 值...
  • 如果您想从两种不同的表单中获取值,您可以在action... 中提供相同的 php 文件,第一种形式:&lt;form method='get' action='shop.php?action=add&amp;id=$i'&gt;,第二种形式:&lt;form method='get' action='shop.php?summary=true'&gt;...我建议使用POST 方法而不是GET,因为您正在管理敏感数据...
  • @Alessandro 我需要加载两个不同的页面,description.php 当单击列表中的任何项目并加载具有新值的同一个 shop.php 页面时。这可能吗?

标签: php html forms input get


【解决方案1】:

尝试使用您需要的值添加操作和 ID 作为隐藏输入字段

编辑: 将您的代码更改为如下所示:

for($i=1; $i<=sizeof($names); $i++) {
        echo "<form method='get' action='shop.php'>";
        echo "<li><a href='description.php?item=$i'> ",$names[$i],"</a></li>";
        echo "<input type='number' name='quantity'>";
        echo "<input type='hidden' name='action' value='add'>";
        echo "<input type='hidden' name='id' value='" . $i . "'>";
        echo "</form>";
    }

【讨论】:

  • 如果没有&lt;input type="sumbit"...,用户必须按 Enter 才能使表单工作...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多