dongquming3255 2018-03-28 11:56
浏览 66
已采纳

在创建PHP会话和重定向之前显示消息

I am trying to show a success message before creating a PHP session and redirecting to another page. The problem is that if I use the sleep() function, once I submit the form it just sleeps for 3 seconds and then redirects the same to the next page without showing the message. Here is the bit of code where I am having this trouble:

if(mysqli_query($connect, $query)){
    echo '<div class="alert alert-success" role="alert">Foi registado com sucesso!</div>';
    sleep(3);
    $_SESSION['email'] = $user_email;
    header("Location: areacliente.php");
}
}else{
    $erro .="O registo falhou!";
}
  • 写回答

1条回答 默认 最新

  • dongpa5277 2018-03-28 14:20
    关注

    What you are trying to do can be done using JavaScript. Also as noted by the commenters, you may want to either have a button or write the message on the next page. It looks like the message isn't critical, so auto-disappearing is probably not a problem:

    Option 1 - JavaScript Redirect:

    Use essentially the same script you have now, but use javascript to redirect.

    if(mysqli_query($connect, $query)):
        # Assign before message
        $_SESSION['email'] = $user_email ?>
        <!-- write message -->
        <div class="alert alert-success" role="alert">Foi registado com sucesso!</div>
        <!-- create timeout -->
        <script>
        setTimeout(function(){
            window.location = 'areacliente.php';
        }, 3000);
        </script>
    <?php else:
        $erro .="O registo falhou!";
    endif;
    

    Option 2 - Message to Next:

    Assign the session and just redirect to the next page, then show the message on that page and auto-hide it on countdown (or not).

    /whatever_file_this_is.php

    # Just set this as default false
    $_SESSION['success'] = false;
    if(mysqli_query($connect, $query)){
        # Set this to true for the next page
        $_SESSION['success'] = true;
        # Set the email as you have it
        $_SESSION['email'] = $user_email;
        # Redirect
        header("Location: areacliente.php");
        # Stop so rest of the script doesn't run
        exit;
    }
    else {
        $erro .="O registo falhou!";
    }
    

    /areacliente.php

    <?php
    # Check if the session success is true
    if(!empty($_SESSION['success'])):
        # Remove it since it's being used now
        unset($_SESSION['success']); ?>
        <!-- Add an id to this div -->
        <div class="alert alert-success" role="alert" id="success-msg">Foi registado com sucesso!</div>
        <!-- count down and hide the message after 3 sections -->
        <script>
        setTimeout(function(){
            document.getElementById('success-msg').style.display = 'none';
        },3000);
        </script>
    <?php endif ?>
    

    展开全部

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

报告相同问题?

悬赏问题

  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部