-3

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

         echo $_SESSION['msg'];
         unset($_SESSION['msg']);
     ?>
   </div>
 <?php endif ?>

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

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

2 Answers2

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

$('#messagediv).show().delay(1000).fadeOut();

Refer to this for more details.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ajitpawarink
  • 436
  • 6
  • 14
0
setTimeout(()=>{ $('.msg').hide(1000); },3000);    

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

stillKonfuzed
  • 402
  • 3
  • 10