如何在不同的PHP文件中使用具有相同变量名的PHP方法$_GET[]


how can i use the PHP method $_GET[] with the same variable name in different php files

在index.php中,我创建了一个表单,并在表单标记的action属性中指定了"page2.php"

page2.php也是一个表单(从index.php继续),在表单tage的action属性中我指定了"page3.php"。我可以在index.php上的表单中检索用户在"位置"文本框中输入的内容,并通过方法在page2.php中显示<?php echo $_GET["location"]; ?>

但是,我现在也想在page3.php中显示用户位置,但当我使用上面的方法时,它不起作用。它给了我一个错误:"未定义的索引:位置"。我认为这是因为page3.php无法访问index.php中的字段,但我该如何实现呢?

提前感谢

您需要存储$_GET["location"]的值;在提交到page3.php.的表单中page2.php上的隐藏字段中

更新示例

page2.php

<form action="page3.php">
    <input type="hidden" value="<?php print $_GET['location']; ?>" name="location_from_page2" />

page3.php

$location = $_GET["location_from_page2"];

您在这里所做的是将location的内容打印到一个隐藏字段的值中,然后在page3.php中读取这个隐藏字段

这是一种方法,但您可能需要考虑查看php会话。

尝试使用会话变量

$_SESSION['location'] = $_GET['location'];

您可以使用隐藏字段来实现这一点。

page2.php:

<form id="myform" action="/page3.php" method="get">
    <fieldset>
        <label>x</label> <input type="text" name="something" value=""><br><br>
        <input type="hidden" name="location" value="<?php echo $_GET['location']; ?>">
        <input type="submit" name="submit" value="submit">
    </fieldset>
</form>

您需要在第二页的表单中包含这些值。

类似:<input type="hidden" name="location" value="<?= $_GET["location"]; ?>">

或者在表单中将变量附加到您的操作中。

否则,您可以只使用会话,这可能是最简单的。