Wednesday, May 30, 2007

Over Refresh

Refresh is a nice feature on browsers to use when you've gone too far, or have updated the style in your page.

However, it can bite you on occasion.

What happens when you refresh a screen is that the browser resubmits the previous screen, and shows the result. This can cause a problem when the world has moved on. Such as when you've just clicked 'Add' to enter a new row of data to your database. On such occasions, the add request will be repeated, and a new and duplicate row will be added.

The first approach to fixing this problem might be to store the newly added row id in the form. If 'Add' has just occurred, and this id exists, then ignore the call.

...doesn't work. Remember, the refresh call is sending a cached set of post data. This will include the old values of any state flags you set to try and avoid the problem. What was right last time, therefore , will be right this time and...

The only solution to this that I have found is to store your state variables as a server side 'session' variable. Now, when your cached post data is received, it is checked against the current session state, which will have stored the fact that you just did an add with this data.

As someone once said: 'Life wasn't meant to be easy'

Sunday, May 20, 2007

Spaced out variables

As Jane Austen might have put it:
'It is a truth universally acknowledged that a young variable name in possession of a whitespace must be want of a bug correction report.'
It's a no-brainer. Something no programmer would contemplate doing, even in their most caffeine-crazed moments. So, when I got a complaint about an option which wouldn't work for a particular entry, I did quite a bit of head scratching and tracing before I spotted the problem.

The option in question was a piece of html representing a checkbox. It was generated via php as part of a report. Its value was taken from a database table, and was used to generate a variable.

This one entry was a reference code which just happened to have a space in it...

A case of what you don't know can hurt you!