【问题标题】:How to get value of a link created on post [duplicate]如何获得在帖子上创建的链接的价值[重复]
【发布时间】:2016-03-15 22:55:17
【问题描述】:

我试图弄清楚如何在 POST 上获取 ?thread 的值。所以如果 ?thread=12;我想在提交帖子时返回该值,然后将其保存在变量中。

线程的价值

<?php foreach($threads as $thread) :?>
<td><a href = "?thread=<?php echo $thread['id'];?>">Thread Name</a></td>
<?php endforeach ; ?>

表格

<form action = "." method = "post">
<input type = "submit" value = "submit">
</form>

【问题讨论】:

  • 要检索 GET 变量,您可以使用超全局变量 $_GET。因此,$thread = $_GET['thread']; 要检索 POST 变量,请改用 $_POST

标签: php forms foreach submit


【解决方案1】:

如果您的网址如下所示:

http://example.com/test.php?thread=12

然后您可以通过 $_GET 访问您的 test.php 文件中的 'thread' 的值。

$value = $_GET[ 'thread' ];
// $value = 12

如果 URL 如下所示:

http://example.com/test.php

你使用这样的形式来传递线程值

<form action = "." method = "post">
    <input type = "hidden" name = "thread" value = "12">
    <input type = "submit" value = "submit">
</form>

然后你可以通过 $_POST 访问你的 test.php 文件中的 'thread' 的值。

$value = $_POST[ 'thread' ];
// $value = 12

我希望这能消除 $_POST 和 $_GET 之间的混淆。

【讨论】:

  • 所以我创建了输入,但问题是我希望我的值是动态的,而不仅仅是静态的。我试过这样做,
  • 输入名称必须与数组索引相同,因此在这种情况下您将检查 $_POST['threadID']。此外,如果您想通过 URL 传递值,您应该使用 $_GET[ 'threadID' ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2016-04-11
  • 2016-04-26
  • 1970-01-01
相关资源
最近更新 更多