Using cookies to auto-open a lightbox form per browser session

Last Update: May 24, 2016

This guide will show you how to use session cookies to automatically open a JotForm lightbox when a user first visits your webpage. You will need access to a PHP enabled Webserver to host your webpage to be able to create this effect.


1. Create your form. 

2. Grab the JotForm Lightbox code for your form by;

(a) clicking on Setup & Embed toolbar then click the Publish icon

(b) clicking Embed and then select the LightBox option.

(c) Copy and paste this code somewhere safe such as an open notepad document. For this example, we are only interested in the code between the two


3. We need to make a small change to this code to enable us to open our lightbox automatically;just change the openOnLoad function, it's after the width set, it should look like this openOnLoad:true .

4. Ok, now we are ready to transfer this code to your webpage. Create a PHP file on your Webserver (as we are using PHP code to create this effect, your will page will use the .php extension.) Mine is called sessioncookie.php.

5. In this page we begin by adding some PHP that will check for a session cookie, if it doesn't exists it creates one and sets a variable that we will use later to know if we need to open our Lightbox form. If it already exists nothing is done (i.e no lightbox form will be displayed).

NOTE: When the browser window is closed, this cookie gets 'destroyed'.


Add this code at the very start of your webpage:


$showlightbox="0";

if(!isset($_COOKIE['beenhere'])) { 

 // FIRST VISIT TO WEBPAGE THIS SESSION

 setcookie("beenhere", '1');

 $showlightbox="1";

}

?>



6. Now, we just need to add one final bit of PHP and our lightbox embed code that we created earlier in Step 3. At the end of our webpage, just before the closing tag, add the following: 

if ($showlightbox) { ?>

 


NOTE: If you looking to create this effect on your own webpage with your own form, you need to change lightbox embed code (highlighted in blue text) to that of your own.

This code, using the variable mentioned earlier, adds the Lightbox code to our webpage (and launches it), if no cookie was found. 

This is a very basic example webpage showing these pieces on code in place:



This code is also available here if you wish to copy and paste sections of it. You can view and test the functionality of my example webpage with the effect in place here.

Questions? Please post it to our support forum or put it as a comment below!


13 Comments...


   
clicksem (January 16, 2012 at 03:43 PM)

nice guide thanks. what if the page we'll put the code is an asp page?

View Answer


   
anudeep kumar (July 01, 2012 at 06:25 AM)

every thing fine working but its coming on every page,once we click home page also its coming

View Answer


   
precisionsails (October 12, 2013 at 04:48 PM)

Is there a solution for this that does not use php?

View Answer


   
nyeesha2007 (November 12, 2013 at 11:07 AM)

What if you dont want the cookie to be destroyed in the code above... What do I need to change? and the lightbulb code works fine in Chrome but not in Internet explorer what could possibly be the problem?

View Answer


   
Anil (February 19, 2014 at 08:42 AM)

Kindly let me know how can i use this to open a particular page of my site. I need same code to open my signup page once a day.

Thanks in advance.

Anil

View Answer


   
tidybooks (March 24, 2014 at 07:36 AM)

Could you advise on how to get it to work within magento, the pop up part works fine having trouble with sessioncookie.php part. Thanks

View Answer


   
ncrs (April 13, 2014 at 06:31 PM)

Is it possible to do this with my website, which was created using Weebly? If so, could you please provide instructions as to how to do it?

Thank you in advance for taking the time to answer.

View Answer


   
WideScenes (April 03, 2015 at 10:02 PM)

Hi , I tried the code as per instructions, however still appears each time page is refreshed within same browser session. can you my page to see if correct? http://www.widescenes.com/Fiji2015.html
thanks

View Answer


   
damiancooper (June 02, 2016 at 07:02 AM)

Hi can you help with my popup form. I have loaded it to a test page it all looks fine with a delay to open, but every refresh of the page it still loads the form. http://www.purestoves.co.uk/extras.html

View Answer


Send Comment