我花了很长时间试图在阅读指南后弄清楚这一点,但我无法理解。我有 3 个文件 - 用户.php、地图.php 和新地址.php。我只包括了我所理解的这个问题的重要部分。如果需要,很乐意提供更多信息。
用户.php将"地图编号"传递给地图.php。
用户.php
<form action="map.php" method="post">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
map.php接收"mapnumber"并从数据库中生成具有匹配"mapnumber"的地址列表。从map.php,用户可以添加新地址,这些地址采用"mapnumber"值并在newaddress.php中处理。
地图.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
newaddress.php 使用"mapnumber"值将地址添加到数据库中,然后重定向回 map.php它应该根据"mapnumber"生成地址列表,但 map.php 不会从 newAddress 中获取"mapnumber".php因此不会生成地址列表。
新地址.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php");
exit;
请帮忙
你可以像这样设置 get 变量:
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?nr=". $mapnumber);
exit;
比在map.php
你得到它:$mapnumber = $mysqli->escape_string($_GET['nr']);
您可以使用 cookie 在远程浏览器中存储值。 但是您遇到的真正问题是位置重定向就是这样。它告诉浏览器加载另一个 URL。 您可以像这样将获取请求添加到该位置重定向 URL:
header("Location: map.php?mapnumber=".urlencode($mapnumber));
但请记住,默认的位置重定向将是永久性的(HTTP状态代码301(,由浏览器重新检查重定向URL/文档是否可能已更改。
http://php.net/manual/en/function.header.php#78470
因此,您可能需要考虑使用 307 重定向:
header("Location: map.php?mapnumber=".urlencode($mapnumber), TRUE,307);
无论如何:将变量从一个页面传递到另一个页面通常不是一个好的做法。尽量避免它并尝试深入研究会话。
http://php.net/manual/en/intro.session.php
我建议您在map中使用get方法.php
然后它将收到像map.php?mapnumber=somenum
这样的映射编号
用户.php
<form action="map.php" method="get">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
地图.php
$mapnumber = $mysqli->escape_string($_GET['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
新地址.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?mapnumber=$_POST['mapnumber']");
exit;