简体   繁体   中英

How do I display a success message after redirecting the user back to my home page?

I have a registration form that uses AJAX and PHP for validation, and I can currently display error messages on the registration page if the user does not correctly fill out the form. When the user successfully registers, he or she is taken back to the home page of my website via:

echo
    "<script type=\"text/javascript\">
        window.location.href='../index.php';                     
    </script>";
exit();

I want to know how to display a success message at the top of my website once this occurs, preferably a message that disappears if the user refreshes the home page after the message has already been displayed.

You can do it in couple ways but the easiest one is to pass an argument to the index.php file

index.php?status=1

and then in the index file, you will check if that argument exists and what value does it have (for example 0 - error, 1 - success)

if(isset($_GET['status'])) {
  if($_GET['status'] == 1) {
    echo "User was registered successfully!";
  }
}

or you can define (on the script that verifies user data and login) a session variable with a success message and display it on the index page

   if(isset($_SESSION['success'])) {
     echo $_SESSION['success'];   
   }

Reminder: Don't forget to call the session_start() function on both pages (index page and in the validation script)

Use sessions so it doesn't show again when you refresh the page.

Serverside:

$_SESSION['success'] = 'Registered!';
echo
    "<script type=\"text/javascript\">
        window.location.href='../index.php';                     
    </script>";
exit();

Homepage:

if (isset($_SESSION['success']) && ! empty($_SESSION['success'])) {
    echo htmlentities($_SESSION['success']);
    unset($_SESSION['success']);
}

Make sure you initialise sessions with session_start(); before trying to manipulate them

那是特定于您的应用程序的,但是我要做的是在成功登录后在PHP会话中设置一条消息,然后在index.php页面加载时检查该会话

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