What I Get For Accomidating Firefox On OS X Keyboard Users

I found this blog post about how to get Firefox in OS X to recognize tabindex:

I just spent the better part of an hour trying to get tabindex to work
under Firefox/Mac OS X.

The solution lies not in Firefox, but in the Mac OS X preferences.

Apple Menu > System Preferences > Keyboard and Mouse >
Keyboard Shortcuts

and under “Full Keyboard Access” choose “All controls”.

Sweet lord….

Now in the first line if you replace ‘an hour’ with ‘the afternoon’ then you’ll get an accurate picture on how I spent my afternoon today.

Sweet lord indeed…

How Truth Tables Ruined My Afternoon

This piece of code was killing me this afternoon:

$DB_ERROR = -1;
$API_ERROR = -2;
$UI_ERROR = -3;

$errorCodes = array($DB_ERROR, $API_ERROR, $UI_ERROR);

$returnCode = TRUE;
$isError = in_array($returnCode, $errorCodes);
print($isError);

I was expecting $isError to be FALSE but it kept turning up as TRUE. I just couldn’t understand it. That is until I read the online docs:

When converting to boolean, the following values are considered FALSE:

* the boolean FALSE itself
* the integer 0 (zero)
* the float 0.0 (zero)
* the empty string, and the string “0″
* an array with zero elements
* an object with zero member variables (PHP 4 only)
* the special type NULL (including unset variables)
* SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource).

Do you see what I was assuming here? I considered TRUE to be a 1 and FALSE to be everything but a 1. I guess I’m just so used to those truth tables with 1 representing true (thank you very much Discrete Mathematics For Computer Science). But hey you’ve gotta work with whatever the language gives you.

At first I thought about converting $returnCode to an integer before I make that in_array comparison but then after reading more of the online docs I found something cleaner:

$isError = in_array($returnCode, $errorCodes, TRUE);

That extra TRUE checks the types as well as the values.

Five Days In The Hospital

Last week I spent 5 days in the hospital. I got into this bicycle accident (sadly no vehicle was involved. Somehow I thought this would make the accident more noble. Or at least less embarrassing). I hit a sidewalk curb and woke up in the ambulance. And in between hitting that sidewalk curb and waking up in the ambulance I manged to fracture 5 ribs and dislocate my shoulder.

So… some words of advice to all my non existent readers out there. First off: If you’re riding your bicycle down the steepest bridge in your city make sure to slow down. Even if you have a green light at the bottom of the bridge. Even if you’ve made that downhill trip a million times before. And especially if you’re rushing to work because you can wait to write some kick ass code. Just remember… slow down!

Secondly… Use JQuery wherever possible. It rocks.

The Double Submitting Form

From the ‘Thank God We Have External Users As Testers’ department comes the following bug: The Project has this one particular form which takes forever (thank you third party API) to complete. Because of the whole ‘Did I Click On That Submit Button Or What?’ feeling that a user may experience when interacting with this particular form, he/she may be compelled to hit that submit button again. Which produces less than desirable results.

The (client side) fix that I found was rather straightforward. Just disable the submit button when the form is submitted:

$('form').submit(function()
{
   $(this).find('input:submit').attr('disabled', true);
});

However… a small problem. When the form is submitted with the disabled submit button, the value of that submit button isn’t included in the POST values. Which kind of sucks because The Project uses the value of the submit button to check if the form should be processed or not:

$processForm = isset($_POST['SubmitButton']);
if ($processForm)
{
   // process your form
}

else
{
   // show an error page or something. NOT supposed to be here
}

I had to figure out another way to check if the form was actually POST‘ed or not. I decided to check whether a POST array exists:

$processForm = isset($_POST) && count($_POST) > 0;

Not as clean… but it works.

As for the server side solution… I needed to track (with a $_SESSION variable) when the form is first submitted and whether or not it’s been completed. That way if the form is double submitted that second submit can be handled:

$processForm = $_SESSION['IsFormBeingProcessed'];
if ($processForm)
{
   $_SESSION['IsFormBeingProcessed'] = TRUE;
   // when the form processing is finished set the tracking to false
   $_SESSION['IsFormBeingProcessed'] = FALSE;
}

else
{
   // forward to a 'Stop hitting that submit button!' page
}

Not even close to being clean… but it works.

Sometimes You Can Please All Of Them

Attention all future potential users of The Project who for some reason or another choose to browse with scripting off: The Project, as it stands now, will still be fully functional. Sure it won’t be as pretty or slick but it’ll still work.

And for all future potential users with scripting on: All of you will have the pleasure of The Project running some of the most consise Javascript I’ve ever written. And it’s all because of JQuery. I haven’t fallen this much in love with a framework since I met Hibernate a few months ago…

Next Page →