因此,在我正在开发的这个应用程序中,它会在我的数据库中搜索一些代码,抓取它,打开一个新页面,并通过获取其 id 将代码(如有必要)放置到新页面上。
现在,在该代码(来自数据库)中,有另一种表单根据用户的输入进行处理。我遇到的问题是新表单未在同一页面上处理。它重定向到一个新的 URL,该 URL 未指定数据库中的代码。例如...
当使用数据库中的代码生成新页面时,网址如下所示...
localhost/newpage.php?id=1
然后,当我从数据库的代码中提交表单时,它会更改为...
localhost/newpage.php?input1=blah&input2=blah
但我想要这样的东西......
localhost/newpage.php?id=1&input1=blah&input2=blah
仅供引用,此代码需要是动态的。例如,假设我不知道用户正在寻找什么 id,并且在该 id 中我不知道有多少个输入字段。
如果你们需要一些明确的代码(显然我遗漏了不必要的东西)...
这是 searchpage.php,它搜索数据库并显示所有相关项目。然后用户选择一个包含 id 的项目并生成具有该 id 的 newpage.php...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
这是从数据库检索并放置在 newpage.php 中的表单...
<form action="newpage.php" method="GET">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
这是newpage.php生成时顶部的php...
if (isset($_GET['id'])) {
//retrieves data from specified id from database
if(isset($_GET['inputs'])) {
//do something with user inputs
}
}
有什么办法可以实现这一点吗?感谢所有帮助!谢谢:)
<小时/>解决方案
为此,我必须创建一个存储 id 的 session 。因此,我必须将所有 $_GET 变量(处理 id 信息)更改为 $_SESSION 变量。但只要您不存储关键/敏感信息,它就可以工作!
这是代码更改...
search.php 中的更改...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
$_SESSION['current_id'] = $id;
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
newpage.php 中的更改...
<form action="newpage.php?id=" method="POST">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
newpage.php 的 php 变化...
if (isset($_SESSION['current_id'])) {
//retrieves data from specified id from database
if(isset($_POST['inputs'])) {
//do something with user inputs
}
}
感谢...
萨蒂克汗
最佳答案
您可以在引用的 header 中获取查询字符串“id”。您可以在客户端或服务器中附加该查询字符串。
请使用$_SERVER['HTTP_REFERER']
获取引用网址,然后您可以从那里获取ID。如果它是登录用户的 ID,出于安全原因,我建议将它们存储在 session 中。
根据您的评论, 您可以采取以下任一行动, 1. 将 ID 作为隐藏变量保留在表单中 2. 使用 id 作为查询字符串之一更新您的 url,并将其发送回客户端以进行错误更正。 3. 将 ID 保留在 session 中(如果适用) 4.使用post并在重定向之前执行表单验证
谢谢。
关于PHP 如何在处理另一个表单后保持在同一页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518992/