PHP 页面 - 将弹出窗口/悬停添加到调查邀请中

发布于 2024-11-14 00:29:37 字数 331 浏览 4 评论 0原文

我想向现有的 PHP 页面添加一个选项,邀请用户参与一项调查 - 我曾在我过去访问过的网站上看到过类似的邀请,但我从未亲自构建过这样的邀请。邀请将是一个悬停的弹出窗口,出现在当前页面的顶部,其中包含进入新窗口/页面的参与选项和简单隐藏邀请的拒绝选项。这项调查只会运行一周,因此我可以在 7 天后轻松删除代码,但它还需要检测用户之前是否访问过此页面,并且不会再次取消邀请。

我假设它将使用 cookie 来检测他们之前是否看过邀请,并使用带有 DIV 的 JavaScript 来显示邀请。我正在寻找任何示例/代码来展示如何在 PHP 页面中执行此操作,以便我可以在我的 PHP 页面上实现此操作。

非常感谢, 史蒂夫

I would like to add an option to an existing PHP page that invites users to participate in a survey - I've seen similar invitations appear on sites that I've visited in the past but have never had to build one myself. The invitation will be a hovering popup that appears on top of the current page with an option to participate which goes to a new window/page and an option to decline which simply hides the invitation. This survey will only run for one week so I can easily remove the code after 7 days but it also needs to detect if the user has previously visited this page and not dislpay the invitation again.

I assume it will use cookies to detect if they've seen the invitation previously and JavaScript with a DIV to display the invitation. I'm looking for any examples/code that shows how to do this in a PHP page so I can implement this on my PHP page.

Many thanks,
Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

打小就很酷 2024-11-21 00:29:37

最简单的方法是使用 JavaScript,我使用 jQuery 来执行 JavaScript。因此,您将创建一个 div,因为您希望调查看起来像这样:

<div id="idOfDiv">Style this and do what you normally would</div>

然后在 CSS 中放置:

#idOfDiv { display:none; }

最后对于 jQuery,您可以使用以下代码片段来显示它:

$("#idOfDiv").fadeIn();  
//You can add a time in the parenthesis of fadeIn in milliseconds
//to speed up or slow down the div loading

如果您想跟踪是否有人关闭它并让假设您有一个 ID 为 close 的关闭按钮,您可以使用 jQuery 来完成此操作。

 $('#close').click(function(){
    $.post('location/of_file/to_set/cookie.php',function(data){
        //If you want to have a confirmation message or something put this here,
        //for after the cookie gets set.
    });
    $('#idOfDiv').fadeOut();
});

在 cookie.php 中,只需:

<?php setcookie('noSurvey','true',time()+5000000,'/'); ?>

最后在您的页面上,您只需放置调查消息的 div:

<?php if(!$_COOKIE['noSurvey']){ /*put div here */ } ?>

这将允许您仅向拥有 cookie 的人显示消息,并且您可以设置 cookie,而无需离开页。此外,在调查页面上,一旦他们完成调查,您可能会想要设置相同的 cookie,这样他们就不会再这样做了。

The easiest way to do this would to be with JavaScript and I use jQuery to do my javascript. So you would create a div as you would want the survey to look like so:

<div id="idOfDiv">Style this and do what you normally would</div>

Then in your CSS put:

#idOfDiv { display:none; }

Finally for jQuery you can use the following snippet to get it to show:

$("#idOfDiv").fadeIn();  
//You can add a time in the parenthesis of fadeIn in milliseconds
//to speed up or slow down the div loading

If you want to keep track if somebody closes it and lets say you have a close button with an ID of close you can do this with jQuery.

 $('#close').click(function(){
    $.post('location/of_file/to_set/cookie.php',function(data){
        //If you want to have a confirmation message or something put this here,
        //for after the cookie gets set.
    });
    $('#idOfDiv').fadeOut();
});

and in cookie.php just have:

<?php setcookie('noSurvey','true',time()+5000000,'/'); ?>

And finally on your page where you have the div for the survey message just put:

<?php if(!$_COOKIE['noSurvey']){ /*put div here */ } ?>

That will allow you to only show the message to people who have the cookie and you can set the cookie without ever leaving the page. Also on the survey page once they've completed it you would probably want to set that same cookie so they don't do it again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文