I am trying to redirect a user to the previous page (topic.php) when they successfully submit a new post. That's easy:
header("Location: viewtopic.php?id=" . $topicid . ");
I may be going about this the wrong way, but on success, i want them to be redirected automatically to the address above and display a success message.
Some of my postreply.php
$result = $database->query($sql);
if ($result) {
header('Location: /community/viewtopic.php?id=' . $topicid . '+status=success');
}else {
echo "Query failed" . print_r($sql);
}
This is on the viewtopic.php:
$statusmsg = $_GET['status'];
if (isset($statusmsg)) {
if ($statusmsg === "success") {
$statusmsg = '<div class="alert alert-success" role="alert">
<strong>Success!</strong> Your post was submitted.
</div>';
}else {
$statusmsg = "";
}
}
Understandably, php thinks the +status=success
is part of the query to fetch the data from the database, thus results in a MySQL error.
Is there a way of doing this?
You should use &
instead of +
when passing parameters from the url to the $_GET
super-global in PHP.
So, you need to change '+status=success'
to '&status=success'
Also, as noted in the comment you should always validate user input sent via $_POST
/ $_GET
.
in your case, you can use filter_var()
if(isset($_GET['success'])){
$statusmsg = filter_var($_GET['success'], FILTER_VALIDATE_STRING);
if($statusmsg) {
echo $statusmsg;
}else{
// unsafe or invalid string passed
}
}
Change here like this:
header('Location: /community/viewtopic.php?id=' . $topicid . '&status=success');
viewtopic.php
$statusmsg = $_GET['status'];
if (isset($statusmsg) && !empty($statusmsg)) {
if ($statusmsg == "success") {
$statusmsg = '<div class="alert alert-success" role="alert">
<strong>Success!</strong> Your post was submitted.
</div>';
}else {
$statusmsg = "";
}
}
header('Location: /community/viewtopic.php?id=' . $topicid . '+status=success');
在此行中的“ +”位置使用“&”。您将能够获取URL中传递的状态值。
Replace +
with &
like this :
header('Location: /community/viewtopic.php?id=' . $topicid . '&status=success');
Now the $_GET['status']
catch the status value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.