我想知道是否有人可以帮助我。
首先道歉,因为我是新手,所以对某些人来说这似乎是一个非常基本的错误。
我使用下面的表单允许用户在所有基于“位置”记录的不同页面之间导航。
<form name="locations" id="locations" method="post" action="locationsaction.php">
<?php
<table width="538" border="0">
<tr>
<th width="119"><div align="left">Location Name</div></th>
<th width="159"><div align="left">Address</div></th>
<th width="94"><div align="center">No. Of Finds</div></th>
<th width="69"></th>
<th width="66"></th>
<th width="5"></th>
</tr>
<tr>
<td><div align="left"><?php echo $lid;?></div></td>
<td><div align="left"><?php echo $lname;?></div></td>
<td><div align="left"><?php echo $laddress;?></div></td>
<td><div align="center"><?php echo $findscount?></div></td>
<td><div align="center"><input name="type" type="submit" value="View Details"/></div></td>
<td><div align="center"><input name="type" type="submit" value="Add Finds"/></div></td>
<td><div align="center"><input name="type" type="submit" value=" Add Images"/></div></td>
</tr>
<tr><th colspan="8"><br/><hr width="100%"></th></tr>
</table>
<?php
</form>
在表单的末尾,您会注意到有三个“提交”按钮,可将用户带到不同的页面。以下 PHP 脚本用于确定选择了哪个按钮,并在上面的 Form POST
action 中显示为 locationsaction.php
<?php
if (isset($_POST['type'])) {
$urls = array(
'View Details' => 'updatelocation.php',
'Add Finds' => 'addfinds.php',
'Add Images' => 'image.php'
);
$url = $urls[$_POST['type']];
header("Location: " . $url);
}
?>
虽然按钮工作正常,因为它们将用户带到正确的页面,但我无法将 $lid
变量传递给三个接收页面中的任何一个,我收到以下错误:
注意:未定义索引:lid in/homepages/2/d333603417/htdocs/updatelocation.php
这就是我试图在所有接收页面中获取 $lid
变量的方式:
$lid = $_POST['lid'];
我浏览了本网站上的几个教程和帖子,但我找不到哪里出错了。
我只是想知道是否有人可以查看此内容并让我知道哪里出错了?
非常感谢和亲切的问候
最佳答案
首先,将 $lid 变量放入输入中:
<input type="hidden" name="lid" value="<?=$lid?>" />
否则不会在表单中发布。
此外,当您重定向时,POST 变量不会随您一起重定向。您可以将它们附加到 url (http://url.tld?lid=value),然后像这样访问它们:
$lid = $_GET['lid'];
但是,更好的方法可能是将它们保存在 session 中:
$_SESSION['lid'] = $_POST['lid'];
// Redirect here
然后在下一页上,您可以访问它们:
$lid = $_SESSION['lid'];
关于php - $_POST 到 PHP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11034111/