Friday, August 01, 2008

A Few Mini Gotchas Of the Day

These aren't particularly serious gotchas...but still, what a pain:

  • Setting a checkbox's checked attribute to empty string still leaves it as checked:
      <input name='foo' type='checkbox' checked=''/>
      <input name='bar' type='checkbox' checked='checked'/>
    
    Both foo and bar from above will result in a checked checkbox. You have to use fun code like:
      <input name='baz' type='checkbox' <?= is_checked() ? 'checked="checked"' : '' ?> >
    
    Yuck
  • The behavior of onchange for a checkbox is different for IE than it is for Firefox (OK, that's not really a surprise). From here:
    I have numerous ajax calls throughout my app, and they work fine in both FF and IE. However, there is one that behaves differently in IE and FF. It seems that the "onchange" behavior of a checkbox happens at different times between the browsers. With FF, the event is sent as soon as the checkbox is checked. In IE, it isn't sent until the user clicks off the checkbox somewhere else on the page.
    The answer? Use onclick instead of onchange
  • And this was the winner of the morning: pg_query, one of (the?) PostgreSQL driver for PHP appears to completely ignore Postgres' view of the boolean type. If you attempt to do:
      UPDATE foo SET bar = ?
    
    and bar is a boolean column, you can't simply send in PHP's version of false. If you do, bar will be set to an empty string, and PostgreSQL will complain about a type mismatch. OK, that's annoying, but not too hard to work around, just do:
      UPDATE foo SET bar = (? <> '')
    
    But, to make matters worse, when pg_query extracts results from a table, it doesn't translate PostgreSQL's representation of false (f) to a PHP false. The result is that if you do:
      SELECT foo FROM bar;
    
    And foo is boolean - you'll end up with the PHP value being either the string 't' or 'f' - which as far as PHP is concerned, are both true.

    The workaround I used was to switch my column from being boolean over to an int and PHP was just happy with it. I think it's uglier to use int when boolean is what's intended, but I'll live with it. I'm just amazed that a fundamental type like boolean is handled in a more clean way (or really, at all). I'm using CodeIgniter for this project, so perhaps it shares some of the blame for this?

Whew, I feel much better getting that all off my chest.

No comments:

Post a Comment