最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hiding flash message after some seconds in codeigniter php - Stack Overflow

programmeradmin0浏览0评论

Hi i have tried with some script hiding the flash message once it is shown but it is not working.Displaying the flash message until i refresh the page.

Controller:

if ($this->email->send())
        {
           $this->session->set_flashdata('msg','<div class="alert alert-success text-center" id="successMessage">Thank you for contacting us we will get back to you soon!</div>');
            redirect('contact');
        }
        else
        {
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
            redirect('contact');
        }

View:

<script>
$(function() {
// setTimeout() function will be fired after page is loaded
// it will wait for 5 sec. and then will fire
// $("#successMessage").hide() function
setTimeout(function() {
    $(".alert-success").hide('blind', {}, 500)
}, 5000);
});
</script>
<div class="container">

<div class="row contactpageback">

        <div class="col-lg-6 contactuspagedetails">
            <form name="contact"  id="contactform" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>contact">
            <?php echo  $this->session->flashdata('msg');?>
            <?php if(isset($msg)){?>
             <?php echo $msg;?>
               <?php } ?>

Hi i have tried with some script hiding the flash message once it is shown but it is not working.Displaying the flash message until i refresh the page.

Controller:

if ($this->email->send())
        {
           $this->session->set_flashdata('msg','<div class="alert alert-success text-center" id="successMessage">Thank you for contacting us we will get back to you soon!</div>');
            redirect('contact');
        }
        else
        {
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
            redirect('contact');
        }

View:

<script>
$(function() {
// setTimeout() function will be fired after page is loaded
// it will wait for 5 sec. and then will fire
// $("#successMessage").hide() function
setTimeout(function() {
    $(".alert-success").hide('blind', {}, 500)
}, 5000);
});
</script>
<div class="container">

<div class="row contactpageback">

        <div class="col-lg-6 contactuspagedetails">
            <form name="contact"  id="contactform" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>contact">
            <?php echo  $this->session->flashdata('msg');?>
            <?php if(isset($msg)){?>
             <?php echo $msg;?>
               <?php } ?>
Share Improve this question asked Jun 7, 2017 at 7:18 user8001297user8001297 1731 gold badge4 silver badges18 bronze badges 3
  • codeigniter./user_guide/libraries/sessions.html#flashdata that's how fashdata works. – Sachin Bahukhandi Commented Jun 7, 2017 at 7:25
  • can't we use jquery to hide – user8001297 Commented Jun 7, 2017 at 7:32
  • sure you can but about flashdata it can't be removed automatically until page is refreshed. – Sachin Bahukhandi Commented Jun 7, 2017 at 7:38
Add a ment  | 

3 Answers 3

Reset to default 2

Is this what you want. Hope this helps you out.

$(function() {
// setTimeout() function will be fired after page is loaded
// it will wait for 5 sec. and then will fire
// $("#successMessage").hide() function
    
    $(".hide-it").hide(5000);

});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<div>
  <h1 class='hide-it'>Flash Data value here</h1>
</div>
</body>
</html>

This will help you to hide your message after 3 seconds with fadeOut animation. You can change seconds in timeout variable.

var timeout = 3000; // in miliseconds (3*1000)

$('.alert').delay(timeout).fadeOut(300);
<link href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="alert alert-success">
  Success Message.
</div>

Controller:

$this->load->library('session');
 if ($this->email->send())
        {
           $this->flash->success('Thank you for contacting us we will get back to you soon!</div>');
            redirect('contact');
        }
        else
        {
            $this->flash->success('There is error in sending mail! Please try again later');
            redirect('contact');
        }

View:

<div id="mydivs">   
        <?php    echo $this->flash->display('success', TRUE);?>
    </div>
    <script> 
        setTimeout(function() {
            $('#mydivs').hide('fast');
        }, 10000);
    </script>
发布评论

评论列表(0)

  1. 暂无评论