Archive for September, 2007

geek stuff: IE6 “Page cannot be displayed” fix in PHP

Thursday, September 20th, 2007

If you're working with forms in PHP, and especially if you're working with forms that have multiple steps, you've probably run across the "back button" problem in IE6. Here's what it looks like:

1) you fill out a form and click the "submit" button - this takes you to another page.

2) you click the "back" button.

3) IE6 takes you to an error page that says:

"Page could not be displayed"

and/or:

Warning: Page has Expired
"The page you requested was created using information you submitted in a form. This page is no longer available. As a security precaution, Internet Explorer does not automatically resubmit your information for you. To resubmit your information and view this Web page, click the Refresh button."

The problem is this: you want to be able to return to the previous page without resubmitting form data, AND with the form data still displayed as you've entered it. This, of course, is a caching problem. There are numerous proposed solutions for this online, but surprisingly few of them work well. Here's the one that works for me:

<?php
header
("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0″false);
session_start();
header("Cache-Control: private");
?>




discovered

Monday, September 10th, 2007

Blessings on the forgotten, and the wonderful, and the strange.



geek stuff: passing parameters to onreadystatechange in AJAX

Thursday, September 6th, 2007

Maybe everyone knows this, but I've seen very little about it online. The vast majority of AJAX-oriented functions looks something like this:

var xmlHttp

function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML=""
  return
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
var url="gethint.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)


This function (taken from a w3schools tutorial) calls a function named stateChanged() when it receives data. stateChanged handles the display of response data, and looks like this:

function stateChanged() 

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}

Note that this function accepts no parameters, and for good reason - if you attempt to pass parameters to it in the first function, like so:

xmlHttp.onreadystatechange = stateChanged('hello world')

… everything will break.

If you need to pass parameters at onreadystatechange (and I often need to), here's how you do it:

xmlHttp.onreadystatechange = function(){ stateChanged('hello world'); };

and then you modify stateChanged() to take parameters as needed:

function stateChanged(param) { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
         document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
     } else {
        document.getElementById("txtHint").innerHTML=param;
    }
}

And Bob's your uncle.