代码之家  ›  专栏  ›  技术社区  ›  Paddy Hallihan

在用户在特定时间段后访问站点后创建弹出窗口

  •  0
  • Paddy Hallihan  · 技术社区  · 8 年前

    我正在我的网站上创建一个弹出窗口,试图让用户注册我们的邮件列表。

    php 在我的所有页面中,当他们在网站中导航超过一分钟时,弹出窗口将出现。

    但是,如果他们停在一个页面上,不点击,我仍然希望它弹出,而不必更改页面或刷新当前页面。

    <?php
    session_start();
    
    if(isset($_POST['mailing_list_input']) && $_POST['mailing_list_input']!=""){
        $email = $_POST['mailing_list_input'];
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $params = [$email];
            $sql = "INSERT INTO mailing_list(email, date_time) VALUES (?,now())";
            $stmt = DB::run($sql,$params);
    
            $date = new DateTime();
            unset($_SESSION['timer']);
            $_SESSION['already_asked'] = $date;
        }else{
            $email_warning = "<div id='warning'>Email address is not valid</div>";
        }
    }
    
    if(isset($_POST['mailing_list_hide']) && $_POST['mailing_list_hide']=="hide"){
        $date = new DateTime();
        unset($_SESSION['timer']);
        $_SESSION['already_asked'] = $date;
    }
    
    if(!isset($_SESSION['timer']) && !isset($_SESSION['already_asked'])){
        $date = new DateTime();
        $date->modify("+1 minutes");
        $_SESSION['timer'] = $date;
    }
    
    if(isset($_SESSION['timer']) && !isset($_SESSION['already_asked']) && !isset($_COOKIE["customer_login"])){
        $date = new DateTime();
        if($_SESSION['timer'] <= $date){
            echo "<div id='popup'>";
            echo "<form class='mailing_list_form' method='POST' action='".$_SERVER['PHP_SELF']."'>";
            echo "<input type='hidden' id='mailing_list_hide' name='mailing_list_hide' value='hide'>";
            echo "<button id='mailing_list_close' name='mailing_list_close' onclick='this.form.submit();'>X</button>";
            echo "</form>";
            echo "<h2>SUBSCRIBE FOR LATEST DISCOUNTS & COMPETITIONS</h2>";
            echo "Stay up to date with our latest products";
            echo "<br /><br />";
            echo "<form class='mailing_list_form' method='POST' action='".$_SERVER['PHP_SELF']."'>";
            echo "<input type='text' id='mailing_list_input' name='mailing_list_input' placeholder='Enter your email address here'>";
            echo "<button id='mailing_list_button' name='mailing_list_button' onclick='this.form.submit();'>";
            echo "<img src='media/icons/subscribe_icon.png' alt='subscribe icon' width='15px' height='15px'>";
            echo "</button>";
            echo "</form>";
            if(isset($email_warning)){echo $email_warning;}
            echo "<p>By continuing you agree to the <u><a href='page/Privacy-and-Cookie-Policy'>Privacy and Cookie Policy</a></u>.</p>";
            echo "</div>";
        }
    }
    ?>
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   gramgram    8 年前

    PHP是一种服务器端语言,(in)活动发生在客户端。您必须在后者上实现您的解决方案,例如JS:

    var popupTimer,
    TIME_OUT = 60;
    
    function displayPopup() {
        // display the popup
    }
    
    popupTimer = setTimeout(displayPopup, TIME_OUT);
    
    $(document).on('click change keypress', function() {
        clearTimeout(popupTimer);
    
        // Reset
        popupTimer = setTimeout(displayPopup, TIME_OUT);
    });
    
        2
  •  0
  •   Paddy Hallihan    8 年前

    这就是我最后想到的。如果他们在站点中导航,它将获得会话计时器,如果页面上的暂停没有点击,它将只使用JS计时器

    <script>
        function displayPopup(){
            $("#popup").fadeIn("slow");
        }
        function hidePopup(){
            $("#popup").css("display", "none");
        }
    </script>
    <?php
    if(isset($_POST['mailing_list_input']) && $_POST['mailing_list_input']!=""){
        $email = $_POST['mailing_list_input'];
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $params = [$email];
            $sql = "INSERT INTO mailing_list(email, date_time) VALUES (?,now())";
            $stmt = DB::run($sql,$params);
    
            $date = new DateTime();
            unset($_SESSION['timer']);
            $_SESSION['already_asked'] = $date;
        }else{
            $email_warning = "<div id='warning'>Email address is not valid</div>";
        }
    }
    if(isset($_POST['mailing_list_hide']) && $_POST['mailing_list_hide']=="hide"){
        $date = new DateTime();
        unset($_SESSION['timer']);
        $_SESSION['already_asked'] = $date;
    }
    if(!isset($_SESSION['already_asked'])){
        echo "<script>var popup_timer = setTimeout(displayPopup, 10000);</script>";
    }
    ?>
    <div id='popup'>
    <form class='mailing_list_form' method='POST' action='<?php echo $_SERVER['REQUEST_URI']; ?>'>
    <input type='hidden' id='mailing_list_hide' name='mailing_list_hide' value='hide'>
    <button id='mailing_list_close' name='mailing_list_close' onclick='this.form.submit();'>X</button>
    </form>
    <h2>SUBSCRIBE FOR LATEST DISCOUNTS & COMPETITIONS</h2>
    <p>Stay up to date with our latest products</p>
    <form class='mailing_list_form' method='POST' action='<?php echo $_SERVER['REQUEST_URI']; ?>'>
    <input type='text' id='mailing_list_input' name='mailing_list_input' placeholder='Enter your email address here'>
    <button id='mailing_list_button' name='mailing_list_button' onclick='this.form.submit();'>
    <img src='media/icons/subscribe_icon.png' alt='subscribe icon' width='15px' height='15px'>
    </button>
    </form>
    <?php if(isset($email_warning)){echo $email_warning;} ?>
    <p>By continuing you agree to the <u><a href='page/Privacy-and-Cookie-Policy'>Privacy and Cookie Policy</a></u>.</p>
    </div>
    <?php
    if(!isset($_SESSION['timer']) && !isset($_SESSION['already_asked'])){
        $date = new DateTime();
        $date->modify("+20 seconds");
        $_SESSION['timer'] = $date;
        echo '<script>hidePopup();</script>';
    }
    if(isset($_SESSION['timer']) && !isset($_SESSION['already_asked']) && !isset($_COOKIE["customer_login"])){
        $date = new DateTime();
        if($_SESSION['timer'] <= $date){
            echo '<script>displayPopup();</script>';
        }else{
            echo '<script>hidePopup();</script>';
        }
    }
    if(isset($_SESSION['already_asked'])){
        echo '<script>hidePopup();</script>';
    }
    ?>