【发布时间】:2019-05-16 19:51:48
【问题描述】:
我是 php 新手,在练习时遇到了一个问题。实际上,我有两个文件 index1.php 和 index2.php。在 index1.php 中,我有一个具有唯一 ID 的链接
<a href="index2.php?companyid=<?php echo $row('company_id');?>>details</a>
我在 index2.php 中得到了这个值
if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
现在我在 index2.php 中有一个搜索表单
<form method="POST" action="index2.php">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
现在点击按钮我希望搜索结果显示在同一页面中
'index2.php?companyid=$companyid'
但是如果我尝试在同一页面中使用$_POST['submit'];,它会将我带到index2.php 而不是index2.php?companyid=$companyid,如果我不使用$_POST['submit']; 和echo $companyid; 它提供了价值并且工作正常。我想要的是使用$companyid' value inside ``$_POST['submit']; as 并在与以前相同的 url 中显示结果
if(isset($_POST['submit']){
$companyid //throws an error index of company id
}
任何帮助将不胜感激
【问题讨论】:
-
您没有在 POST 请求中传递公司 ID,因此在新页面加载时它不存在。 PHP 是无状态的。它不记得请求之间的任何内容。为了“记住”事物,您可以将它们作为查询字符串值传递,将它们作为 POST 请求中的请求主体参数发送,或者使用 cookie 作为存储机制。在您的情况下,您可能希望将公司 ID 作为 URL 查询字符串的一部分。