Easy Surveys and Polls with a Movable Type Weblog


Published on February 27, 2006 at 5:49 AM EST
In the Tutorials category.

A tasty dessert filled with chocolate, caramel, and PHP-goodness, smothered in fresh HTML and CSS

Update: A better way to add polls is with the Poll Position plugin!

An easy way to integrate a survey system with Movable Type is something I’ve wanted for a while. Learning Movable Type’s Polls and Voting article describes two methods of adding such things to a website, but it doesn’t really integrate. Clearly, a better solution is needed.

This tutorial will help you create an MT-based survey and polling system that uses the familiar Edit Entry screen and comments to store the votes. Using a separate weblog, some PHP, and these templates, you can have your own survey/polling/voting system in place quickly and easily!

You might want to try participating in a survey to see how it works, too.

Background

If you’re not interested in how I “got here,” you can just skip by this section and get started.

When I was first thinking about building this website, I wanted to make it stand out from the other Movable Type community websites. I had two ideas of how I wanted to do that: with a unique style (which became this restaurant theme) and frequent, fun surveys.

The Polls and Voting article on Learning Movable Type provided me with some quick and easy ways to set up surveys, but nothing that really integrated well with MT. A while later the book Hacking Movable Type came out with a chapter devoted to creating a weblog for polls, complete with the code to make it work.

Unfortunately, I could never get the templates from Hacking MT to work, despite fooling with it on and off for several months—and this site was missing a key element to make it fit my original vision. Then, one day, it ocurred to me that using some PHP, I knew exactly how to build a simple polling system. About an hour later I had this system worked out!

Create a New Weblog

Surveys need their own weblog to work, so you’ll need to create a new one. Create a new weblog (from the Main Menu, click Weblogs and Create New Weblog, then give it a name and directory). After saving changes, go to your new weblog’s Settings screen.

  • In the New Entry Default Settings screen you’ll need to change Text Formatting to None and be sure Accept Comments is checked, then save changes.
  • In the Feedback Settings screen, choose Accept Comments from: Anyone. Do not check the Require E-mail Address box. Choose to Immediately Publish Comments from: Anyone. Lastly, Text Formatting should be set to None, and save the changes.
  • In the Publishing Settings screen change the File Extension for Archive Files option from html to php and save your changes.

Update Templates

The templates are really the core of what makes this survey system work. Below are the updated templates. Copy and paste them into the appropriate template type; be sure to save each one.

Main Index

Category Archive

Date-Based Archive

Individual Entry Archive

Creating a Survey

http://www.eatdrinksleepmovabletype.com/upload/2006/02/new-entry-thumb.gif

Just as you’re probably used to, go to the New Entry screen to create a survey. You can refer to the screenshot at the right, if it helps.

  • Add a title and, if you want, a category.
  • The Entry Body field should contain your survey question. Because Text Formatting is set to None, you must enclose your text in HTML. If you’re not familiar with HTML, just follow this example to add the paragraph (<p></p>) tags:
    <p>What is the survey question?</p>
  • The Extended Entry field holds the voting options. Put each option on a new line. You can use some HTML to format the options, but do not wrap them in paragraph tags.
  • Publish and Save!

To end the “voting period” go back into this entry and uncheck the Accept Comments checkbox near the bottom of the screen.

A Little More Detail

This survey system works by assigning a number to each voting option. When somebody votes they’re actually making a comment through MT’s commenting system, and this comment is a number. Those numbers get added and tallied to create the survey results.

Voting is allowed or disallowed based on the visitors IP address. It’s not a perfect system, but it was easy to implement.

I’ved added two little pieces of easy customization to the survey templates. At the top of the Individual Entry Archive templates are two variables you can change: $bar_width and $colors.

  • $bar_width is the width of the bar graph that gets created for the results, and is specified in pixels.
  • $colors is an array of the colors to use for the bar graph. There must be at least as many colors listed as their are options for your survey. There are currently 10 colors listed, so your surveys shouldn’t contain more than 10 options (of course, fewer is fine). If you want to have more options, you’ll want to add more colors—just follow the format established.

This article is tagged as: Comments, CSS, HTML, PHP, Surveys, Template, Tutorials

Comments

So far, there are 15 comments and Trackbacks on this entry. Add yours!

1

Thanks, this is *exactly* what I was looking for! A cinch to implement, and so far working perfectly.

2

Nice idea but I followed your instructions and on building the templates they errored thus:

An error occurred:

Build error in template ‘Main Index’: Error in tag: You used an tag outside of the proper context.

And the url gives me a 404?!

3

Further info - I found the Survey (expected it to be in the blog root: jamesspratt.com/blog/vote/

http://www.jamesspratt.com/blog/vote/archives/2006/07/

But now there are parse erros (related to the rebuild errors?)

Would appreciate your help - thanks, James

4

This sounds like exactly what I am looking for. however I have other customisations in my templates which I will loose if I just copy your in their entirity. Any chance you can just list the relevant edits, so I (and others) can just paste in those sections?

5

Mite—

Since you have to create a new weblog anyway, I figure that copying my templates in works well. Make your template modifications after implementing mine.

Or have you updated the default template files? Yeah, I can see the trouble that might cause. The Main Index, Date and Category templates are simply to display the survey title. The Individual Entry Archive is where the work happens. Basically, you need all the PHP (<?php … ?>) and everything within the entry class.

6

Ack, my bad. I didn’t see that it was a new weblog! That makes total sense now. Thanks I am going to try it right now.

7

Wow, that was so simply to set up! Thanks for your hard work. If onlyI had read the instructions better the first time I visited your site ;) It works perfectly and is exactly what I was looking for. Now all I have to do is incorporate the polls into my main site using MTOtherBlog, which is pretty straightforward to do also.

Thanks again!

8

Is there any way to allow visitors to vote as many times as they want, rather than allowing/disallowing votes based on IP address?

Thanks!

9

Thanks for this great tutorial. It works great.

I’m trying to integrate this separate survey blog into my main blog. While I’ll maintain the permalinks and the individual survey pages, I’d like people to be able to vote on the main page of my main blog.

So far, I have the survey blog output an individual archive template that mostly mimics the actual individual archive template; I then include that new archive within my main blog.

So far, so good. People an vote on the main blog page, and when they do that, they’re taken to the results page. When they go “back” on their browsers, though, the survey-input form remains there, and they’d need to reload the page for it to show the results.

I don’t care too much that people can vote multiple times using this method. What I’d like to accomplish, though, is for the main blog page to automatically reload when they submit a vote. The vote would be recorded, and the results would then show, and they wouldn’t be able to vote multiple times.

Put another way, how does one change where the “vote” button takes one in the code, and how does force the page to refresh?

Any help would be appreciated.

10

Culture Snob, and others,

You may be interested in the changes I’ve made to this system, which address some of the issues posted here.

http://slapnose.com/archives/2007/02/14/creating_polls_in_movable_type/

11

I finally accomplished most of what I wanted. If anybody cares, the basic method is to create an second individual archive template that outputs text files for each poll. Those files can then be included in another blog. (See how it works at my Web site.)

There were two main challenges:

1) The comment script in Movable Type automatically takes one to the individual archive page for the poll, so even if people can vote on an integrated page, they’re still led away from the main blog.

This is easily corrected. Replace the default individual-archive template with a simple redirect, such as this:

<head>

<meta http-equiv=”refresh” content=”0; URL=http://www.yoursite.com/yourfrontpage.php#<$MTEntryID pad=”1”$>”>

</head>

2) If there are multiple surveys on one page, there’s an error because the survey and results functions aren’t uniquely named. This is easily fixed, too. In the template, simply use the entry ID to give them unique names:

survey()
results()

Just be sure to re-name every instance of those functions.

I’m guessing there are more elegant ways to do this, but this worked well for me.

12

Hello, I’ve been trying to adapt a poll to my blog using the steps outlined above. I see by your latest survey that it seems that you’ve managed to integrate it into the main entry field - when I try to set it up on my blog, it asks me to vote or see the results on another page. Is this normal or am I missing a step? Any help or insight would be greatly appreciated. Many thanks.

13

Ok, this works pretty darn well :)
However, I really dont want to display the polls name and question with a “vote on this” or “view results” link. I want to display the questions themselves on my main blog’s page.

How can I display the contents of the individual entry archive instead of the main index for this poll?

I have tried a meta refresh and sent the refresh to but it doesn’t seem to work.

Ideas?

14

Any word on how well this works w/ MT4?

15

It works equally well with MT4, I tested it and the results were pretty much ok.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

 
 
 


TrackBack URL for Easy Surveys and Polls with a Movable Type Weblog:
http://www.eatdrinksleepmovabletype.com/cgi-bin/mt/mt-tb.cgi/430