user3488706
user3488706

Reputation: 11

PHP, submit form and redirect using header and echo out a success message?

I am trying to echo out a div with the message "form submitted successfully" on the next page once a user has submitted a form.

i am trying to do this by using $_GET['success'] but can not seem to get the message to display, can someone please point me in the right direction.

code:

submit_form.php:

session_start();
$_SESSION['success'] = "<div class='success'>Form Submitted Successfully</div>;
header("Location: index.php?success=$success");

index.php:

<?php
$_GET['success'] ?>

Upvotes: 0

Views: 6874

Answers (6)

Veda
Veda

Reputation: 2073

The amount of errors in your code is huge:

submit_form.php:

<?php
session_start();
$_SESSION['success'] = "<div class='success'>Form Submitted Successfully</div>"; // added " at the end
header("Location: index.php?success"); //Do NOT pass HTML in here. That makes an XSS security vulnerability 
?>

index.php:

<?php
session_start(); //ALWAYS start with session_start if you use sessions. session_start should be at the very beginning of your page.
if(isset($_GET['success']))
    echo $_SESSION['success']; //you put your data in _SESSION, you echo it from _SESSION
?>

Upvotes: 0

Neeraj Kumar
Neeraj Kumar

Reputation: 504

It should work 100%.

First page:

header("Location: index.php?success=done");

Index.php page:

if(isset($_REQUEST['success'])=="done")
{
    echo "<div class='success'>Form Submitted Successfully</div>";
}

Upvotes: 0

Neeraj Kumar
Neeraj Kumar

Reputation: 504

There is not need to use first two line in you first page. Just use redirection as you have used it on your third line and display message on your next page "index.php" like this:

First page:

header("Location: index.php?success=done");

Index.php page:

if(isset($_REQUEST['success'])=="done")
{
    echo "<div class='success'>Form Submitted Successfully</div>";
}

Upvotes: 0

sandip
sandip

Reputation: 3289

You are storing the value in the variable $_SESSION['success'] and passing $success which is undefined so the the error is thrown

Replace you code with this code:

session_start();
$success = "<div class='success'>Form Submitted Successfully</div>"; // use the $success
//encode the URL parameter as : 

$success = urlencode($success);

header("Location: index.php?success=$success");

And in index.php just echo success as:

 <?php 
    echo isset($_GET['success'])?$_GET['success']:'Error!'; 
  ?>

Hope this will help!

Upvotes: 0

Andy Holmes
Andy Holmes

Reputation: 8077

As you're already setting a session variable, just echo that out in your index.php file

session_start();    
<?php echo $_SESSION['success']; ?>

Upvotes: 0

user3497295
user3497295

Reputation:

session_start();
$_SESSION['success'] = "<div class='success'>Form Submitted Successfully</div>;
header("Location: index.php");

you are setting the session with text message so all you need to do this is to echo the session variable on index.php page

<?php
    if(isset($_SESSION['success']))
    {
        echo $_SESSION['success'];
    }
  ?>

Also put session_start(); at the beginning of every page.

Upvotes: 2

Related Questions