PHP pass variable after form is submitted
I have a form that posts to process.php. Process.php send all the data to mysql then returns back to the page using:
<?php header("Location: /campaigns"); ?>
On the page 开发者_JAVA技巧it returns to I want to display a message saying form submitted. Can I post a variable to the Location: /campaigns. And use the variable to display the message (or modal box)? and if so what would be the best way to do it?
There is many ways to do that, but here you got two most popular
1. Using GET
Just add GET variable to your URL that inform that the forum has been submitted successful:
header('Location: /campaigns?success=1');
...
if (isset($_GET['success']) && $_GET['success'] == true) {
echo 'Hurra!';
}
2. Using session variables
$_SESSION['success'] = true;
header('Location: /campaigns');
...
if (isset($_SESSION['success']) && $_SESSION['success'] == true) {
echo 'Hurra!';
}
Use a get parameter, like /campaigns?message=Your%20stuff%20was%20saved%20successfully%2E
that you then evaluate in campaigns.php
. But be careful: The user can write whatever he wants in that parameter, so you should html escape it etc.
you'd better have that variable in place. Sessions is another option, a cleaner one.
精彩评论