简体   繁体   中英

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'] ?>
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.

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

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

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!

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>";
}

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

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>";
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM