Hosting Wufoo forms on your own server
20·Apr·07
Online form-building service Wufoo provide a fantastic set of tools for creating forms quickly and easily. Here’s how to host them on your own server using the Wufoo API.
Yesterday I posted about Wufoo, a very cool form-building web application (see that post). As I mentioned, I find more and more to love about the service each day.
The only small stickler for me as a web designer is that I’d like the forms to be hosted on my own server, rather than on Wufoo’s. Currently you can either have the whole form page hosted with them, or you can cheat a little and use an iframe to call in the form itself into a page on your own site. I understand why this has to be so, and it’s not too much of a problem.
However, as iframes invalidate a strict DOCOTYPE, and they’re not great for accessibility, I decided to once and for all get to grips with using the Wufoo API to host my Wufoo forms on my own server. I can still have the forms processed, checked and validated by Wufoo, with access to all the beautiful reporting, but I also keep it in the family (so to speak).
There’s a great example of how to get started with this on the Wufoo site, but I wanted to give a bit of a step-by-step walkthrough for the more, erm, programatically-challenged – you know who you are. More than anything, I want to have a record of the process that works for me. My instructions are far less generally useful than the run-through from the Wufoo guys, but they’ve helped me get the job done.
Although it takes a mere minute to get a form up and running with the standard iframe option provided by Wufoo, it still only takes a paltry 5 minutes using the API, which is peanuts! Onwards…
Building the form itself
Log into Wufoo and create a form. Build a simple contact form for now, 2 fields only. Here's one I prepared earlier in approximately 30 seconds (hosted on the Wufoo servers at this stage). Save your form.
On the next screen, choose the 'Integrate it into a website or blog post' option. Then choose 'Integrated Form Code' from the 3 options presented to the right.
Copy and paste the URL provided within the provided code (in my example case here it was http://definition.wufoo.com/embed/contact-form/) and visit that URL in a browser. 'View source' - this contains the standard HTML form code we'll need. Easy. Nice one Wufoo!
Preparing the basic page
Copy and paste the form code itself only. Everything between the opening and closing form tags, basically. Here's one I made earlier, pasted directly from Wufoo's admin interface:
<form id="form3" class="wufoo" method="post" action="" enctype="multipart/form-data"> <ul id="formFields"> <li> <label class="desc" for="field0">Email address</label> <div> <input id="field0" name="field0" class="field text medium" type="text" maxlength="255" value=""/> </div> </li> <li> <p class="instruct">Here's a helper instruction for the user.</p> <label class="desc" for="field1">Message</label> <div> <textarea id="field1" name="field1" rows="10" cols="50" class="field textarea small"> </textarea> </div> </li> </ul> <div class="buttons"> <input id="submit" name="submit" class="button" type="submit" value="Submit" /> </div> </form>Visit the stylesheet reference given in the form source code (e.g. /css/public.css). Copy and paste the CSS styles, and pop them in a new CSS file. Put this on your server and link to it, just as you link to your other stylesheet(s). The provided CSS is very well documented and commented, as a great base for further alterations.
Note there are some extra IE7 styles at /css/global/ie7.css which again you may want to have as default anyway. This how I'd link 'em up on my server:
<!--[if IE 7]> <link rel="stylesheet" href="/css/browsers/ie7-form.css" type="text/css" /> <![endif]-->Copy the code at /scripts/public/dynamic.js and again link to this as normal:
<script type="text/javascript" src="/js/dynamic.js"></script>Save this lot as, say, contact-form.php. You could call it whatever you like.
You now have a beautifully styled form page on your own server. Let's hook it up!
Preparing the form processing files
We'll come back to our actual form page in a minute. First, let's prepare the 2 files we'll need to handle processing the form data and get that out of the way. I've created a forms folder on my server just to hold these general form processing files. Note: the actual form page itself can go anywhere, just nice to have the 2 files responsible for processing in one convenient place.
Go to http://wufoo.com/docs/api/submit/ and grab the latest example files from the Download The Files link. It'll be easier to work from these than to start from scratch.
Rename example-process.php to form-process.php. No reason really, just that we're going to use this file regularly, so might as well give it a more permanent name!
Log-in to your Wufoo account. Click the Account tab and then click the API Information button (like this one):

Take note of your API Key number, plus have a look at the table underneath it. Mine looks like this:

You'll need to know the corresponding ID number for each of your fields, plus the form name (in this case, contact-form)
Open form-process.php in your favourite text editor. Whizz down to line 9, and alter the following code for your server:
require_once($root.'/forms/json.php');If you remember, I've created a forms folder in my site starter files, in which json.php can sit.
Scroll down now to line 22 and see the following function:
function setPostParams(){ $wufoo_query_vals = array( 'w_api_key' => '0000-0000-0000-0000', 'w_form' => 'name-of-form', '0' => $_POST['Field0'], '1' => $_POST['Field1'] ); return $wufoo_query_vals; }Replace the API Key with your own, and change the form name to yours (see the table given on your own Wufoo API page, like the example in the image). Then add the ID numbers for your form fields, as appropriate. In our example, there are only 2 form fields, but you might have many, many in your own form. Just add them on as required, e.g.
'0' => $_POST['Field0'], '1' => $_POST['Field1'] '2' => $_POST['Field2'] '3' => $_POST['Field3'] '4' => $_POST['Field4']Although this definitely covers requirements for most fields, you'll definitely want to check out the definitive list in the API docs here to see how to format trickier fields like date and phone, if you've included them in your form.
Last thing in this file: go down to line 46 and alter the URL to reflect your own username. For me, it's definition so I have mine like this:
$ch = curl_init("http://definition.wufoo.com/api/insert/");For our simple purposes we're now done with form-process.php and don't need to alter anything in json.php, so go ahead and upload these 2 files to your server. As mentioned, I've put mine in a folder called forms just to keep things tidy.
Finally, we haven't touched example.php in the examples folder at all, but you can just trash that file. Really. It's not that it's not useful, but I've already plundered the juicy bits for you (coming next… the excitement!) and we don't need the distraction of the contents of the rest of the file.
Plugging it all together
Now we need to get it all working together.
Open up your prepared contact-form.php file. Pop the following code into the very top, before the DOCTYPE:
<?php require_once($root.'/forms/form-process.php'); ?>Obviously, alter the path accordingly for where you've put it on your server.
Now we need to finally put some small bits of PHP in place, in each form field, to allow validation and error-reporting to function correctly. Here's a sample bit of code, which is the entirety of my first form field:
<li <? echo addErrors('field0', $fieldErrors, 'class') ?>> <label class="desc" for="field0">Email Address</label> <div> <input id="Field0" name="field0" class="field text medium" type="text" maxlength="255" value="" /> </div> <? echo addErrors('field0', $fieldErrors, 'message')?> </li>You'll notice there's merely the addition of 2 bits of PHP: one within the opening list tag itself, and one directly before the closing list tag. The field number (in this example, field0) corresponds with the field number assigned by Wufoo for this particular field (see your API page under your Account tab). Just go through each form field and add those bits of PHP in, changing the field number to the one assigned to it by Wufoo.
Finally, to get the error-reporting actually reporting back to the user, simply add the following bit of code to just inside the opening of the unordered list (but before the very first list item):
<?php // general 'you've got errors' message if (sizeof($fieldErrors) >0) echo "<li id=\"errorLi\"> <h3 id=\"errorMsgLbl\">There was a problem with your submission.</h3> <p id=\"errorMsg\">Errors have been <strong>highlighted</strong> below.</p> </li>" ?>
Now just upload your new contact-form.php page, visit in a browser, and see it in action. Here's the one I made - lovely isn't it?
That about wraps it up
That's the very, very basics of what Wufoo has to offer. Don't forget, you could just as easily create a form in Wufoo and have it called into your page via an iframe, in seconds. We've only used the API so that we can host the form on our own server, and avoid an iframe altogether. We still get Wufoo's marvellous form processing, validation and error checking, and can give clients access to a wonderful reporting system too.
Steamlining the process
Obviously, once you've been through this process once, you might like to standardise it to save time for future forms. I'd keep basic copies of the CSS, PHP and javascript files in a standard place, keeping all paths and references as constant as possible. Then really the only work will be to create the form, grab the code, paste it into place, make a few quick parameter or value changes, and that should be it.
Taking this further
Obviously the API is a powerful thing, and I've merely scraped the very surface of what you could achieve. Our example is very simple - you'll definitely want to be checking out the rest of the instructions given in the Wufoo Submit API docs, as well as the info in the general API docs too. Hats off to the Wufoo guys for creating a fantastic programming interface to an already brilliant product.
Grab the journal RSS feed
#1 | Karin said:
YOU ROCK!!! Thank you for writing this up. It’s just what I was looking for!!! :)
1 year, 7 months ago
#2 | said:
No problem at all Karin, glad it hit the spot!
1 year, 6 months ago
#3 | said:
Hey, do you know how to make the errors script? Like the one that appears:
“There was a problem with your submission.
Errors have been highlighted below”
With the informations:
Email address *
“Please enter a valid email address.”
How to write it? I´d like to make something like that! With the highlighted box!
(Not using the API...)
1 year, 6 months ago
#4 | said:
Good question! I didn’t cover that bit in the article.
To get the general ‘You’ve got errors’ message at the top of the page I added this to the top of the list that contains the form fields (as the first list item):
<?php // general 'you've got errors' message
if (sizeof($fieldErrors) >0) echo "<li id=\"errorLi\">
<h3 id=\"errorMsgLbl\">There was a problem with your submission.</h3>
Errors have been highlighted below.
</li>"
?>
In my example it’s the Wufoo API that puts the correct error message next to each field (e.g missing email address), basically using the last bit of code in the article.
Let me know if you need any more help.
1 year, 6 months ago
#5 | said:
Thank’s Dave!
And without using the API? Is there a way for me to have the errors highlighted? Is it possible?
Do I have to make it one by one writing the message (e.g. “Please enter a valid email address.") ?
In the public.css we have:
/* ----- ERRORS ----- */#errorLi{width:97%;background:#fff;border:1px dotted red;margin-bottom:1em;text-align:center;}#errorMsgLbl{margin:7px 0 5px 0;padding:0;font-size:125%;color:#DF0000;}#errorMsg{margin:0 0 .8em 0;color:#000;font-size:100%;}#errorMsg strong{background-color: #FFDFDF;padding:2px 3px;color:red;}form li.error{background-color: #FFDFDF !important;border-bottom:1px solid #EACBCC;border-right:1px solid #EACBCC;margin:3px 0;}form li.error label{color:#DF0000 !important;}form p.error{color:red;font-weight:bold;font-size:10px;margin:0 0 5px 0;clear:both;}
But I do not know how to make it in the php script…
Just figuring how…
1 year, 6 months ago
#6 | said:
No sorry, I’m not in a position to help : (
1 year, 6 months ago
#7 | Luyza said:
Uuuufff, finally worked ^^ I’ve been working on this for a while, when I decided I should probably google it.... :-)
Thank you! Now my site is all pimped out.
1 year, 3 months ago
#8 | said:
Excellent! Really glad I could help : )
1 year, 3 months ago
#9 | Eugene said:
Hi,
Great tutorial!
Is there a way to have a drop down where a user/customer is able to choose which department he’d like to send the information to?
Like the following?
http://www.dreamincode.net/forums/showtopic2232.htm
Thanks
Eugene
7 months, 2 weeks ago
#10 | said:
Hi Eugene - my honest answer is that I don’t know. I haven’t actually used this method for a good while now (for various reasons) so haven’t had a lot of chance to solve issues like that.
Sorry I can’t help - the Wufoo guys might be able to though?
Good luck :)
7 months, 2 weeks ago
#11 | Chinese SEO Services said:
It’s fantasy ! thanks for introducing the WUFOO,it’s name just like YAHOO :-)
6 months, 1 week ago
#12 | Preethi said:
WUFOO!! sounds and looks good, the article is too good and it explained how it works to create a web form. Great tool, i would like to try this one out.
6 months ago
#13 | said:
First, thank you for the great instructions!
I have noticed that on your form (http://www.fuzzyness.co.uk/examples/wufoo/form.php and mine too) that when a user submits a data that incorrect (in this case a bad email address) and hits submit they are alerted that they did with all the proper alerts but then all the data all clear. Does anyone know how to maintain it?
Thanks again!
4 months ago
#14 | said:
Hey Josh - glad you liked the instructions. Unfortunately I really don’t know the answer to your question. Could be worth firing the Wufoo guys an email? I’ve found them to be very responsive and helpful. Hope you get an answer!
4 months ago