0

I am trying to display a success message when a form is submitted. I did that by

<?php
    if(isset($email)){  
        echo "<p class='alert alert-successful'>Thank you, we will be in touch soon</p>";
    }
?>

But this way, the message stays on the page even when the page is refreshed. How do I display a success message that gets deleted after a refresh?

I am only just learning WordPress and php, I am probably doing this all wrong. I don't want to use a plugin because I am only collecting an email (and sending the info to admin email), and using a plugin seemed like an overkill.

  • Assuming you are looking in the admin, start with Ajax. – Nathan Powell May 28 '18 at 7:10
1

You’re looking for Flash Messages. Flash Messages are messages stored in session and deleted right after they are retrieved. You can find some implementation on github: https://github.com/plasticbrain/PhpFlashMessages

<?php 

// Start a Session
// Read about managing session with WordPress

// Instantiate the class
$fm = new \Plasticbrain\FlashMessages\FlashMessages();

// Process form e.g. send an email and add success message
$msg->success('Thank you for sending message!');

// Display message
if( $fm->hasMessages('success') ) {

    /**
     * This line will display success message and also remove it
     * from session. After refresh $fm->hasMessages('success')
     * will return false
     */ 
    $fm->display('success'); 
}
  • Thank you, I'll look into this. Part of my problem is that I don't know what exactly I am looking for, so this is a great help. – Newbie May 29 '18 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.