doumu8217 2019-03-05 21:14
浏览 106
已采纳

在重定向后使用会话创建弹出消息将不起作用

First of all i would like to mention that this is my first time creating a thread here so i am sorry if i'm doing something wrong.

Anyway i am working on my own forum system and i've got to a conclusion that i need to show popup messages after user actions whether it's a success message or a failure message i need that system on my forum.

I've made a big research and tried many methods to get this type of a system but the method that made the most sense in my case was using sessions as the trigger and also to store the message itself.

Without further ado, this is what i came up with :

function popupMessage(){
    setTimeout(function(){ 
        setTimeout(function(){ 
            $('.popup_message').remove('');
        }, 3000); 
        $('.popup_message').fadeOut();
    }, 3000);
    $('.header').append('<div class="popup_message success"><span><i class="fa fa-fw fa-check-circle"></i></span><p>success</p></div>');
}

This function is on my js file which holds all of my functions.

if(isset($_SESSION['message'])){
    $message = $_SESSION['message'];
    echo '
        <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
        <script type="text/javascript" src="/assets/myscripts.js"></script>
        <script type="text/javascript">
            var message = \''.$message.'\';
            popupMessage()
        </script>';
    unset($_SESSION['message']);
}

That piece of code is on file which is included on the header of every page on my forum.

$_SESSION['message'] = 'success!';
header("Location: $location");

And those 2 rows are beeing executed for an example when a form is submitted.

Tbh i am kind of stuck right now and i dont know what to do and i'll tell you why, the function popupMessage() is working well by itself but it'll simply wont work when i call it through the if(isset($_SESSION['message'])) condition. and it's also important to add that when i've tried to replace the function call in a simple alert() it was working fine which means that the message is set but the function just wouldn't run when it's beeing called through the condition.

Btw i guess that it's pretty clear that javascript or jquery ain't my strong side i am pretty new to these so i am sorry if i've made your eyes hurt or anything.

Thanks in advanced for any help!

  • 写回答

1条回答 默认 最新

  • dongqiao3927 2019-03-05 22:02
    关注

    You need to make sure you're calling popupMessage() below where the HTML of the popup message is created in the DOM due to the order of operations. Otherwise, you can load it in the $(document).ready(); function like so

    $(document).ready(function() {
      popupMessage();
    });
    

    to ensure the HTML element exists before executing JS on it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站