views:

83

answers:

3

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 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?

+1  A: 

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!';
}
Crozin
If you submit success=1, you shouldn't check $_GET['success'] == true, you should check $_GET['success'] == 1.
Charles
@Charles AFAIK, it will return false :)
Col. Shrapnel
@Charles: There could be "1", 1, "true", true - PHP is weak-typed language. But the most proper would be '1', cause every GET variable is a string.
Crozin
While the check against true will cast the one to a boolean for the purposes of the comparison, your code is no longer clear and obvious as a result. Any value that casts to boolean true will be acceptable, and that may be undesired behavior later.
Charles
A: 

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.

Marian
A: 

you'd better have that variable in place. Sessions is another option, a cleaner one.

Col. Shrapnel