0

I have a web form (form.php) in PHP which has following layout:

<form method="post" action="mailer.php" class="form-horizontal">

mailer.php has a function that checks for data, validates it and send email to me. After this all is done successfully I want to redirect to my form page with an alert/info message.

Mailer function in mailer.php looks like:

    $httpReferer = $_SERVER['HTTP_REFERER'];
    print_r("Form submitted successfully. We will contact you soon !");
    header('Location: '. $httpReferer) ;

I want to get the message in print_r on form.php in the following div,

<div class="alert alert-info">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    Make sure to fill all the fields with correct values before sending your email!
</div>

I am able to redirect back, but without any message. Tried echo, tried using JavaScript alerts etc. None worked for me.

3
  • @Chris Thanks for the edit, do we always highlight filenames ? Commented Jan 19, 2014 at 13:47
  • Use session and store you message in a variable, for example : $_SESSION['message']. Or use Cokkies and then store your message in a variable too, for example : $_COOKIE['message']. Commented Jan 19, 2014 at 13:47
  • 1
    @CodeMonkey, many users use backticks for file names (and I find it much easier to read). Some people bold them instead (or as well). I wouldn't have bothered editing just for that, but the print_r and div deserved backticks, so I made the other changes too. Commented Jan 19, 2014 at 13:49

2 Answers 2

1

In your mailer.php script, you can either print a message or use header(), you can't do both. If you want to redirect to form.php and include a message with it, you can either use $_GET or $_SESSION. Using $_GET would look like this:

$httpReferer = $_SERVER['HTTP_REFERER'];
$message = "Form submitted successfully. We will contact you soon !";
header('Location: '. $httpReferer . "?message=" . urlencode( $message ));

Then your div would look like this:

<div class="alert alert-info">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <?php echo $_GET['message']; ?>
</div>

But I think the $_SESSION solution would be cleaner because it won't clutter the address bar with your message:

$httpReferer = $_SERVER['HTTP_REFERER'];
session_start();
$_SESSION['message'] = "Form submitted successfully. We will contact you soon !";
header('Location: '. $httpReferer);

<? session_start(); ?>
<div class="alert alert-info">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <?php echo $_SESSION['message']; ?>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

$_SESSION is coming out to be NULL
The original answer assumed you already had sessions started elsewhere in your scripts, but I've modified the answer with the appropriate session_start()s.
That works. Thanks. Can i also change classes somehow from the mailer script ?
You can set as many $_SESSION variables as you'd like in mailer.php, and them use them in form.php, so that would probably be a good place to hold your classes if that's where you want them to have an effect.
0

instead of redirecting with php do it with javascript. replace header('Location') with,

echo <<<SCRIPT

<script>
setTimeout(function(){

document.location = '$_SERVER[HTTP_REFERER]';   
}, 2000);

</script>

SCRIPT;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.