Radar Hill Blog

Living with crappy code

After you’ve inherited a crappy and poorly programmed website from someone else, that was done by a ‘lousy programmer’, sure enough the site owner will ask for ‘stuff’ to be done.  Here we go!

“When I open a summary of my clients I need a checkbox beside their information.  Then when I’ve checked all the clients I’m interested in I want to be able to send a batch email to them.”

No big deal, right?  We POST and collect that selection of clients using a SQL.  Well, what if there are four different forms defined on the page?  A page full of JavaScript?  And, the administration menu is outside of the form collection?

You knuckle down and try your hand at JavaScript?  Ask Jamie, another PHP programmer here at Radar Hill, to have a look. He looks at the code we inherited and replies with “ah, um, no thanks”.  You take the plastic off the sliding glass door behind you (put up last winter to stop the cold draft on your back) and consider jumping off the deck.  Ok, I’ll try the JavaScript.

So I need the child page ( aka pop-up window ) to ‘read’ the checkboxes left behind on the parent page that are ‘on’*.  Then I need to invoke an ‘onLoad’ process to write the values of those client IDs into the form used by the child page using client side scripting.  Piece of cake.

<script type=”text/javascript” language=”javascript”>
function checkforchecks()
{
var i;
for( i = 0 ; i < window.opener.document.deleteform.elements[‘mdelete[]’].length; ++i )
{
if ( window.opener.document.deleteform.elements[‘mdelete[]’][i].checked==true )
{
document.write(“<input type=\”hidden\” name=\”mdelete[]\” value=\”” + window.opener.document.deleteform.elements[‘mdelete[]’][i].value + “\” />”);
}
}
}
</script>

Ok.  Got that down.  Now you discover that the ‘lousy programmer’ before you has used PHP SESSION variables to pass a SQL string along to pull the client list.  Shouldn’t be a problem, right?  Nope.  window.opener is a wonderful little piece of code that lets you relate between parent and child pages but doesn’t allow SESSION variables pass.

Oh-oh.  Google!

Someone else has fallen on this sword before.  Dare I say ‘lucky me’?

<script type=”text/javascript” language=”javascript”>
function windowOpen()
{
window.open( ‘customer_batch_email.php’ , ‘bEmail’ , ‘height=500,width=900,scrollbars=1’ );
}
</script>

Turning window.open into function such as the one above will allow SESSION variables to persist.  Well, at least until the next user agent upgrade.  Maybe I’ll be selling tooth paste at the demo kiosk of Costco by then.  I left myself lots of clues and ‘in-script’ notes because sure as Shawn will show up for a free meal this will fail:  next week, next year?

Thanks for listening.  Anyone want my job?

Roger Whitson
Radar Hill Director of Product Development and Lead Programmer

*This is the useful part.  You might need to detect state of HTML form elements just as I describe.  Or, you may live a happy uneventful life where you’re never cursed with this.

About Roger

Director of Product Development

This entry was posted in Website Launches and tagged . Bookmark the permalink.

Comments are closed.