FortySeven Media

Kick Awesome Web Design, Graphic Design & Media Creation

Creating a Tell-a-friend Pop-up Form with jQuery and ExpressionEngine

Thursday, December 10, 2009 ~ 5:59 pm

I recently had a reader ask about the pop-up contact forms and lightbox scripts we've used on some of our client sites - were they built into ExpressionEngine and how did we get them to work? Well the answer to the first question is generally no. EE lets you setup whatever js effects with whatever libraries you want. So can choose exactly how you use it. It really is a different way of thinking about putting together dynamic websites, especially if you're used to most of the other CMS's out there.

So for this example I'm going to show you how to create a "Tell a Friend" box that fades in and auto populates with the page you're currently on. It's a bit of HTML & CSS, a bit of jQuery, and a bit of ExpressionEngine. Ready to get started? Let's go!

For those of you who just can't wait, here's a demo of the static version and a download of the full ExpressionEngine version.

1. Start with HTML

What we want to do first is make this work with a static HTML page. So we need an anchor for jQuery to pull off of and the pop-up box with form fields in it. Something like this:

<h1>ExpressionEngine &amp; jQuery Tell-a-friend Pop-up Box</h1>
<p class="info">Published Nov 23, 2009 <em>by</em> 
<strong>Jonathan Longnecker</strong> <em>in</em> 
<a href="#">Blogging</a></p>

 <p>It's pretty easy to setup. Just create a hidden div with the 
 appropriate fields and show it on click with some jQuery. 
 Then use ExpressionEngine to make the dynamic parts happen.</p>

 <div class="social_links">
    <ul>
       <li><a class="subscribe" href="#">Subscribe</a></li>
       <li><a class="twitter" href="#">Tweet About It</a></li>
       <li><a class="email" href="#emailpopup">Email Someone</a></li>
    </ul>
 </div>     

 <div id="tellfriend" class="contact_form">
 <a class="close" href="#close" >Close</a>
 <form id='tellafriend_form' method="post" action="#"  >

    <label for="from">Your Email: </label>
     <input class="std_input" type="text" id="from" name="from" 
     size="40" maxlength="35" value="" />

     <label for="name">Your Name: </label>
     <input class="std_input" type="text" id="name" name="name" 
     size="40" maxlength="35" value="" />

     <label for="to">To: </label>
     <input class="std_input" type="text" id="to" name="to" 
     size="40" maxlength="35" />

     <label for="subject">Subject: </label>
     <input class="std_input" type="text" id="subject" 
     name="subject" size="40" value="Check out this blog entry 
     from FortySeven Media" />

     <label for="message">Message: </label>
     <textarea id="message" name="message" readonly="readonly" 
     rows="18" cols="40">Summary of Entry Here
     Link to Entry Here
     </textarea>

     <input type="submit" name="submit" class="form_but" 
     value="Submit"/>
</form> 
 </div>

2. Throw in a pinch of CSS

Since we're going to show and hide with jQuery the CSS is just used for positioning and z-index position:

#tellfriend {
    position: absolute;
    margin-top: -300px;
    left: 400px;
    width: 380px;
    z-index: 1;
}

3. Add some jQuery

So obviously we first need to link to the jQuery library in our header (apologies for the line break):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2
/jquery.min.js" type="text/javascript"></script>

Then we can either put this bit of javascript in the header of our document or in a separate file. Whatever's most manageable for you.

jQuery.fn.fadeToggle = function(speed, easing, callback) {
return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};

$(document).ready(function() {
    $('#tellfriend').hide();
    $('li a.email, #tellfriend a.close').click(function() {
    $("#tellfriend").fadeToggle('slow');
  });
}); 

It basically just hides the #tellfriend box on load and shows or hides it when you click the li a.email, #tellfriend or a.close elements. At this point, we should have something like this (Static Demo). I've prettied up a bit, but the basics are there.

4. Mix it up with some ExpressionEngine

So you can see that I've manually put in the subject line and message, but I want to have those pull dynamically as well as actually send an email. Static HTML can't do that. So this is what we add:

{exp:weblog:entries weblog="your_blog" 
disable="member_data|pagination|trackbacks"}

<h1>{title}</h1>
 <p class="info">Published {entry_date format="%M %j, %Y"} <em>by</em> 
 <strong>{author}</strong> <em>in</em> {categories backspace="1"}
 <a href="{path=/blog/}">{category_name}</a>, {/categories}</p>

{body}

 <div class="social_links">
    <ul>
       <li><a class="subscribe" href="#">Subscribe</a></li>
       <li><a class="twitter" href="#">Tweet About It</a></li>
       <li><a class="email" href="#emailpopup">Email Someone</a></li>
     </ul>
 </div>     

 <div id="tellfriend" class="contact_form">
 <a class="close" href="#close" >Close</a>
{exp:email:tell_a_friend charset="utf-8" allow_html='n'}

      <label for="from">Your Email: </label>
      <input class="std_input" type="text" id="from" name="from" 
       size="40" maxlength="35" value="{member_email}" />

      <label for="name">Your Name: </label>
      <input class="std_input" type="text" id="name" name="name" 
       size="40" maxlength="35" value="{member_name}" />

      <label for="to">To: </label>
      <input class="std_input" type="text" id="to" name="to" 
       size="40" maxlength="35" />

      <label for="subject">Subject: </label>
      <input class="std_input" type="text" id="subject" name="subject" 
       size="40" value="{title}" />

      <label for="message">Message: </label>
      <textarea id="message" name="message" readonly="readonly"
       rows="18" cols="40">
         {summary}
         {url_title_path="blog/archives"}
       </textarea>

 <input type="submit" name="submit" class="form_but" value="Submit"/>

{/exp:email:tell_a_friend}    
 </div>

{/exp:weblog:entries}

First of all make sure your {weblog:entries:tag} is wrapped around the entire thing. I've left a few other goodies in the EE template, but the gist is you use short tags to call the info dynamically. Then wrap the {exp:tell_a_friend} tag around your form (replacing your form tags) and insert other dynamic variables like {member_email}, {member_name} (for those already logged in), {title}, {summary} and link to the article. The cool thing is you could put any custom field tied to this content channel that you want.

So there you have it. The box will have it's fields populated and ready to send. Easy to put together and infinitely customizable! You can download the full EE file here.

  • Social Web
  • E-mail

Your Email:*  

Your Name:*

               To:*

Submit the word you see below*

Posted by (JavaScript must be enabled to view this email address) in Design ~ Tutorials ~ ExpressionEngine ~ (9) Comments ~

So Helpful. Thanks for the post.

Thanks Jeremiah! Love your work, btw.

Hi, exactly what i was searching for! but unfortunately can get it working,
here is my testing link:  http://www.gruppoimmobiliarecasa.com/gelone/dettagli_immobile/
Please advice.

Hi, sorry, but i left a comment yesterday and no reply?

@Lincoln, there’s nothing in your #tellfriend div. Patience there, buddy; we’re all busy wink

Hi Jonathan,
sorry for putting pressure on you, it wasn’t my intention, so please when you have time can you please help me to sort this issue.
once again, i’m sorry.
Thanks

No problem. Like I said; it doesn’t look like there’s anything in your #tellfriend div. It’s empty. That’s why nothing is firing.

nice.
but is there a way to ajaxify the form action as well?
so that it doesn’t go to the EE response page
but just displays a confirmation message in the “tellafriend"div (replacing the form)?

thanks!

@pirco, I’ve been thinking about that a lot lately myself, trying to save on page loads and stuff. The problem is if they’ve left something out it might go to the error message template instead of the “return page” that EE sends you to. I’m sure it would be easy if I actually knew enough about AJAX smile. Believe me, if I figure it out, I’ll do a post on it or update this one.

Thanks for reading!

Leave Your Thoughts Behind

Name*

Email*

Website

Smileys

  Remember my personal information

  Notify me of follow-up comments?

Submit the word you see below*

Live Preview

 
Separator
Design Hope for Startups 2009
Stuff we Love

BestChristianDesign is helping give churches inspiration for their next project. Good call, guys.

Seth Godin reminds us to enjoy and take advantage of the uphills.

Stop announcing your new project. Just do it.

Rock and Roll people, Rock and Roll.

To all you staff designers out there, here’s a link for you. (via @ryanlascano)

Fascinating look into the music business.

Derek Sivers posts some great advice about balancing your life.

archives

Rad Deals
Apryl Lynn - With This Breath

Apryl Lynn
With This Breath

Buy in iTunes
Tweets
profile image

Hahaha oh man some good Kick Awesome Show questions are coming in! #tkas

Tue 9:50 | Follow
profile image

The Three Bears is now playing on Happy Story Time! The old stories are on the archive page. Yay! #fb http://happystorytime.com/

Mon 12:43 | Follow
Tweets
Recommended

ExpressionEngine

Basecamp