Solution 1 :

Best way to achieve this using jQuery.

Include Jquery in your app

Create a message div with style display:none by default.

On button click add on click function to change content of that div with your message you can use ajax request to retrieve that message from PHP.

Once you change content use javascript method to show & hide after interval


Refer to this for more details.

Solution 2 :

setTimeout(()=>{ $('.msg').hide(1000); },3000);    

Hides class msg with 1s animation in 3s. Assuming you are using jquery library.

Problem :

I want to output text once a button is clicked but also have it automatically disappear within a specific time frame. Currently, I have written php code so that the message appears when the button is clicked but I am not sure how to make it automatically disappear within a given time frame. The following is the php code I have:

     <?php if (isset($_SESSION['msg'])): ?>
   <div class="msg">

         echo $_SESSION['msg'];
 <?php endif ?>

$_SESSION[‘msg] is obtained from another file and its code is shown below:

    if (isset($_POST['submit'])) {
  $_SESSION['msg'] = "Data Entry Added!";


Comment posted by john greymon

@RobinSingh what would the code look like?

Comment posted by this

Please check