PHP将URL重定向到包含变量的页面 [英] PHP redirect to page with variable in URL
问题描述
我有一个页面, q.php
,这是一个用户提交的帖子,由它的id定义(例如, q.php? id = 1
会是一个使用 $ id
变量从数据库中提取所有其他信息的特定帖子。)
我试图在同一页面上编写评论提交代码,并说明用户可能不会在该字段中输入任何内容。如果发生这种情况,我想让页面(例如 q.php?id = 1
)再次载入错误信息。
我可以用一个空变量来做错误消息,然后通过表单激活的php文件给出一个值。但是,我在导航回特定帖子时遇到问题。我尝试使用 include('q.php?id = $ id)
$ id被设置为一个数字,但我明白这不是它的目的,不接受变量
我应该用什么来代替?
编辑
$ p $ require' q.php;
$ _GET ['id'] = $ id;
$ _SESSION ['error'] =请填写所有字段。;
q.php:
if($ _SESSION ['error']!== 0){
echo< p style ='color:#AA1111;'>。$ _ SESSION ['error' ]。 < / A> 中;
unset($ _ SESSION ['error']); //这是没有发生的......
}
如果你真的必须包含页面内联,你总是可以修改 $ _ GET
:
$ _ GET ['id'] = $ id;
需要'q.php';
但是错误消息听起来像是可以通过会话变量或重定向完成的。重定向可能类似于:
header('Location:q.php?id ='。$ id。'&误差= +的错误');
exit();
然后,检查 $ _ GET ['error']
在 q.php
中。使用会话变量可能会非常相似,除了将 error
作为查询字符串参数添加外,您可以使用 $ _ SESSION ['error']
和取消设置
立即。
I have a page, q.php
, that is a user submitted post defined by its id, (for example, q.php?id=1
would be a certain post that uses the $id
variable to pull all the rest of the information from the database).
I am trying to code a comment submission on the same page, and account for the fact that a user might not enter anything into the field. If this happens, I want the page (e.g. q.php?id=1
) to load again with an error message.
I can do the error message with an empty variable that is then given a value by the php file that the form activates. However, I am having a problem navigating back to the specific post. I tried to use include('q.php?id=$id)
where $id is set to a number, but I understand that this is not its purpose and it does not accept variables
What should I be using instead?
EDIT
answer.php (file that the form activates):
require 'q.php';
$_GET['id'] = $id;
$_SESSION['error'] = "Please fill in all of the fields.";
q.php:
if ($_SESSION['error'] !== 0) {
echo "<p style='color: #AA1111;'>".$_SESSION['error']."</a>";
unset($_SESSION['error']); // this isn't happening...
}
If you really must include the page inline, you can always modify $_GET
:
$_GET['id'] = $id;
require 'q.php';
But an error message sounds like it could be accomplished with a session variable or a redirect. A redirect could look something like:
header('Location: q.php?id=' . $id . '&error=The+error');
exit();
Then, you check for $_GET['error']
in q.php
. Using a session variable for that would be much the same, except instead of adding error
as a querystring parameter, you use $_SESSION['error']
and unset
it immediately.
这篇关于PHP将URL重定向到包含变量的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!