I was tasked with creating a pop-up survey for a project. Fairly simple whether I used the jQuery dialog or not so I added a “Thank you” dialog that displays the survey results with a lightweight, css-based bar chart just for fun.
There is also an ASP.NET version of this post.

Pop-up Behavior
- Pop-up survey opens when page loads. Pop-ups on window.unload or window close are being blocked by most browsers due to abuse and overuse.
- Cookie is set when survey submitted or user opts out (“No, thanks” click).
- Closing dialog will not set cookie
All survey data is stored in a database and jQuery .post is used to shuttle the information back and forth.
Code for the survey dialog:
<div id="survey" title="Pop-Up Survey">
<p id="surveyDenied"><a href="#">No, thanks</a></p>
<p>Pop-up survey that cookies browser on completion or on opt-out. Short 411 demo survey.</p>
<form id="popup_survey" name="popup_survey" method="post">
<p><strong>Pink or blue?</strong><br />
<input id="pink" type="radio" name="radio_color" value="pink" />Pink<br />
<input id="blue" type="radio" name="radio_color" value="blue" />Blue</p>
<p><strong>Soccer or futbol?</strong><br />
<input id="soccer" type="radio" name="radio_sport" value="soccer" />Soccer<br />
<input id="futbol" type="radio" name="radio_sport" value="futbol" />Futbol</p>
</form>
<div id="error_message"></div>
</div>
It’s just a couple of radio buttons and a place for an error message. The submit is handled by the dialog button.
Here is the survey dialog code:
$(function(){
$('#survey').dialog({
bgiframe: true,
autoOpen: false,
modal: true,
width: 500,
resizable: false,
buttons: {
Submit: function(){
if($("input[name='radio_color']:checked").val() !== undefined && $("input[name='radio_sport']:checked").val() !== undefined){
setCookie('POPsurvey','POPsurvey',30);
$.post("process_survey.php", $("#popup_survey").serialize(),
function(data){
if(data.db_check == 'fail'){
$("#error_message").html("<p>Database not available. Please try again.</p>");
} else {
$("div.pink").css("width",data.perPink);
$(".perPink").html(data.perPink + "% (" + data.totalPink + ")");
$("div.blue").css("width",data.perBlue);
$(".perBlue").html(data.perBlue + "% (" + data.totalBlue + ")");
$("div.soccer").css("width",data.perSoccer);
$(".perSoccer").html(data.perSoccer + "% (" + data.totalSoccer + ")");
$("div.futbol").css("width",data.perFutbol);
$(".perFutbol").html(data.perFutbol + "% (" + data.totalFutbol + ")");
$(".totalRes").html(data.totalRes);
$('#survey').dialog('close');
$('#survey_thanks').dialog('open');
}
}, "json");
}else{
$("#error_message").html("<p>Please answer all questions.</p>");
}
}
}
});
});
Lots of stuff but it does several things. First, on submit, it checks that the user answered both questions. Then it sets the cookie. And, finally, it sends the form data off via post and puts the response in elements on the page that are part of the “Thank you” dialog. Speaking of which…
The “Thank you” dialog code is thus:
<div id="survey_thanks" title="Pop-Up Survey - Thank You!">
<p>Thank you for taking the time to answer our survey. Your input will help us improve the site.</p>
<p>Responses: <span class="totalRes"></span></p>
<div class="progress-container">
pink <span class="perPink"></span>
<div class="pink"></div>
blue <span class="perBlue"></span>
<div class="blue"></div>
</div>
<div class="progress-container">
soccer <span class="perSoccer"></span>
<div class="soccer"></div>
futbol <span class="perFutbol"></span>
<div class="futbol"></div>
</div>
</div>
$(function(){
$('#survey_thanks').dialog({
bgiframe: true,
autoOpen: false,
modal: true,
width: 500,
resizable: false,
buttons: {
Close: function(){
$(this).dialog('close');
}
}
});
});
Processing the Survey
The process_survey page adds the answers to the database and brings back the totals that are calculated by a view.
$dbhost = 'YOUR_SERVER';
$dbuser = 'YOUR_USERNAME';
$dbpass = 'YOUR_PASSWORD';
$dbname = 'YOUR_DATABASE_NAME';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$db_check = 'OK';
$return_arr = array();
if (!$conn)
{
$db_check = 'fail';
}
if ($db_check === 'OK'){
$insert_sql = mysql_query("INSERT INTO survey (color,sport) VALUES ('".$_POST['radio_color']."','".$_POST['radio_sport']."')");
if (!$insert_sql) {
$db_check = 'fail';
}
}
if ($db_check === 'OK'){
$fetch = mysql_query("SELECT * FROM v_totals");
/* Retrieve and store in array the results of the query.*/
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$return_arr["totalPink"] = $row['Pink'];
$return_arr["perPink"] = round($row['Pink']/($row['Pink']+$row['Blue']),2)*100;
$return_arr["totalBlue"] = $row['Blue'];
$return_arr["perBlue"] = round($row['Blue']/($row['Pink']+$row['Blue']),2)*100;
$return_arr["totalFutbol"] = $row['Futbol'];
$return_arr["perFutbol"] = round($row['Futbol']/($row['Futbol']+$row['Soccer']),2)*100;
$return_arr["totalSoccer"] = $row['Soccer'];
$return_arr["perSoccer"] = round($row['Soccer']/($row['Futbol']+$row['Soccer']),2)*100;
$return_arr["totalRes"] = $row['Total'];
}
/* Retrieve and display the results of the query. */
}
else {
$db_check = 'fail';
}
/* Free connection resources. */
mysql_close($conn);
$return_arr["db_check"] = $db_check;
echo json_encode($return_arr);
The totals for the return data are stored in a view to prevent separate and multiple calls to the database. The table and the view can be created in MySQL as such:
CREATE TABLE IF NOT EXISTS `survey` ( `id` int(11) NOT NULL AUTO_INCREMENT, `color` varchar(4) DEFAULT NULL, `sport` varchar(6) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; CREATE VIEW `v_totals` AS select (select count(`survey`.`color`) AS `Expr1` from `survey` where (`survey`.`color` = 'pink')) AS `Pink`,(select count(`survey_2`.`color`) AS `Expr2` from `survey` `survey_2` where (`survey_2`.`color` = 'blue')) AS `Blue`,(select count(`survey_2`.`sport`) AS `Expr3` from `survey` `survey_2` where (`survey_2`.`sport` = 'futbol')) AS `Futbol`,(select count(`survey_2`.`sport`) AS `Expr3` from `survey` `survey_2` where (`survey_2`.`sport` = 'soccer')) AS `Soccer`,(select count(`survey_2`.`id`) AS `Expr4` from `survey` `survey_2`) AS `Total` from `survey` limit 0,1;
Cookies
The code to handle setting, checking, and deleting cookies is standard javascript:
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function checkCookie(c_name)
{
cookie_value=getCookie(c_name);
if (cookie_value=="") {
$('#survey').dialog('open');
}
}
function deleteCookie(c_name) {
document.cookie = c_name +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
The style for the “Thank you” graph:
div.progress-container {
border: 1px solid #ccc;
width: 150px;
padding: 1px;
margin-bottom: 5px;
background: white;
}
div.progress-container > div {
height: 12px;
margin-bottom: 2px;
}
div.progress-container > div.pink {
background-color:#CC6699;
}
div.progress-container > div.blue {
background-color: #3366CC;
}
div.progress-container > div.soccer {
background-color: #006633;
}
div.progress-container > div.futbol {
background-color: #663333;
}
You’ll notice in the .post function that the width of the bar graph is set with the percent of responses for each answer that comes back from the database.
If this post helped you out, please consider donating to help pay the hosting fees. 100% of the donations go to the web host.


Twitter
About
46 Comments
Hi Jen
I am trying to do an exit survey and I dont want it to load on page load, like the one you have given here. I am trying to look for an opt out or pop up the survey to those ppl who try to exit the browser or go to some other page. can you help me here?
Thanks
Anuradha
Hey Jen, do you have something like that that works for asp.net pages? if you do – can you share? thanks, you rock!
@Tomek
I have done it in ASP.NET. I'll post it as soon as I can.
Awesome! Christmas came early! Would that be too much if I asked for some modifications (I am actually looking for something that pops up with a slight delay – say a couple of seconds – instead of onLoad)? Also is there a way to somehow make the code global (say, a user comes into the site and the poll appears after a certain period of time regardless of which page is he/she on)? thanks a bunch, you are amazing!
@Tomek
You can delay it with a javascript timer and put the code in a separate javascript file for loading on multiple pages.
I might be amazing but it does not pay the bills when I work for free.
Thanks.
Love this script! awesome! many thanks.
I am trying to modifying the script but not to do so,
I want only one selectable option like, at the moment in your script there are two mandatory:
option "Demo 1"
option "demo 2"
option "demo 3"
option "demo 4"
and that results display from DB, Can you help me?