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
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.
Hope this helps,
Jeff
Edit also you will want to work on your acceptance rate if you want more people to answer. That acceptance rate is proportional to the amount of answers you get :)
精彩评论