Wednesday, May 23, 2007

Roll your Own Simple AJAX Survey for Blogger

Update: With the recent addition of Forms to google spreadsheets, I think this tip is rendered obsolete


There are lots of companies out there that let you embed free and fancy survey widgets for your blog. However, I didn't want to sign up for anything and just wanted a simple way to get user feedback for my plugins site. I hacked together a YUI powered survey with a simple php backend to keep track of user responses. For me, the key to neat hacks is leveraging existing "stuff". In my case I have a blogger-powered site, gmail accounts and my own web server.


How it works:
Once the user selects an option from the survey and hits "Vote", the php script tied to the form's post action sends an email to my gmail account. Because I normalize the subject line with the survey name and the option selected, I can create quick gmail filters. Another freebie is that Gmail groups these similar messages and therefore getting a total-count is just a glance away...

The Javascript half of this is really quite simple. I embedded it all into a single html page so that I could get around the fact that you cannot easily include javascript in blogger posts. By embedding the Javascript in an html page, you can use this clever workaround, and embed the widget using an iframe.

I am using a standard YUI Dialog object, however, to have this work exactly the way I wanted, I needed to override a few of the "interesting moments", as Yahoo likes to call them. Specifically the hide() and validate() methods:


...
//prevent multiple votes, while adding some flair
myDialog.hide = function() {
var attributes = {
height: { to: 25}
};
var anim = new YAHOO.util.Anim('myDialog', attributes, 1.0,
YAHOO.util.Easing.backOut);
anim.animate();
this.setHeader('Thanks for your Feedback!');
};
//prevent en empty vote:
myDialog.validate = function () {
var data = myDialog.getData();
if(data.choice == null) {return false;}
else return true;
};

Right Click and choose Save As here to download the whole html/javascript file.

For those wondering, I chose YUI because we use it at work and it rocks the house. The developers are wizards, and YAHOO! even lets you choose to have the JS and CSS files hosted on their fast servers.
--
The only remaining tricky thing is that you'll need access to your own WWW server. For security purposes, AJAX and POSTS cannot be made to different domains (edit: unless you set up a proxy), so the .html and the .php files (or whatever your favorite server side scripting language is) must reside on the same server.

In PHP, I just grab the survey name and the option the user chose and use sendmail.
...
$surveyname = $_POST['surveyname'];
$surveychoice = $_POST['choice'];
$subject = 'iPhoto2Gmail Survey: '.$surveyname. ' '.$surveychoice ;
...
//set up your message and headers
mail('my-email-address@gmail.com', $subject, $message, $headers);


After that all you have to do is create some filters in your email client and you'll have a super simple way of quantifiying user response -- and by using some YUI animation, it looks pretty slick too!

Wednesday, May 16, 2007

jils@WWDC 2007



Wow.

I've been wrestling with the decision to attend Apple's WWDC this year for about three months now. Ever since my work on iPhoto2Gmail sparked a huge interest in Cocoa programming, I've been trying to figure out which decision will have the greatest overall positive effect on my life and career.

Last Friday, just a couple of hours before the early registration prices expired, I decided to go. My wife and family had no small role in the decision, and I am glad to have their support.

I should say that this will be my first professional conference. I am completely stoked and quite overwhelmed at the same time. It is a strange sensation, but I like it.

I've downloaded the head-starts that seem most interesting to me and have started singling out sessions that I want to attend -- Aperture, Dashboard and Dashcode, Cocoa 2.0, Javascript and SOAP in Cocoa, Cocoa UI Animation, CoreImage, Digital Video, etcetera. I also cannot wait to meet some of the folks that build the awesome software and run the key sites I use everyday. It's going to be one hell of a week!

Now I just have to figure out some minor details. For example: should I risk being stoned and ostracized for taking my only laptop (an aging Dell 600m), or should I sell my Jeep to pay for a Macbook? Will I drive my brother-in-law nuts by crashing at his place for a whole week?

You know, small stuff like that... but it feels right, you know?

Thursday, May 3, 2007

It Truly Is a World Wide Web

For me, a really cool part of having a couple of public websites is getting to interact with folks from all over the world. This might sound like a cheesy cliché, but in my case it has proven to be true. This "dev" site gets very little traffic, but the plugins page gets quite a few hits.

Because I find myself getting more and more interested in neat ways of displaying data, I tried to incorporate some sparklines into my sites. However, the lack of gd on my server, and not wanting to use an SVG-based solution put that on hold for the time being.

For now, this static picture of a random segment of 500 consecutive hits to the plugins page will have to suffice.