Tech Musings

Monday, November 20, 2006

Refresh on Form Update

I finally managed to create a workaround to address a recurring problem I've been having using the php header ("Location:") function to refresh an admin page after updating a record using a form submission. The purpose of the workaround was to create an auto refresh on the visions admin page that would display a record's updated data in a drop-down field after the record was modified. This would alleviate the need for the user to manually reload the page to see the changes. I've successfully used the PHP header function to refresh a page after an update from time to time, but it wasn't working correctly in this particular case. In fact, the server was throwing the dreaded "Cannot modify header information - headers already sent" complaint that annoys the absolute bejeezzes out of me. My first attempt to address the issue was to turn output buffering "ON" in my php.ini file, but this caused another headache with dynamic GD image thumbnail creation on my photo gallery admin catalog page; I needed to find another solution. After racking my brain for a few hours I ended up using a combination of a META "Refresh" directive to temporarily jump away from the admin page to a secondary page, and then a javascript onLoad event in the second page to redirect back and force the admin page to reload itself and display the updated information in the drop-down menu. The trickiest part was passing the user ID variable from the first page to the second page and then back to the first page.

>>>FIRST PHP PAGE >>>

if form is submitted...

$tid = $_POST['tid']; //Get the ID

update the information...
on success ...

echo '<~META HTTP-EQUIV="Refresh" Content="0; URL=refresh.php?tid='.$tid.'"~>';
die;


>>>SECONDARY REFRESH PAGE >>>


<~ head ~>

function goToURL() {
var url = null;
url = "visions_modify.php?tid=";
url += "";
window.location = url; }

<~ /head ~>

<~ body onload="goToURL();" ~>

processing...


<~ /body ~>
<~ /html ~>

UPDATE (6/26/07)
I was having difficulty today with the visions modify pages (lcd and comp) fields not refreshing properly on update and holding entered data; the data wasn't sticking and wouldn't display until a second browser refresh was performed. I implemented the fix above but it didn't work until I added the following code to the top of the page. I'm not sure WHY I needed to add this code, but I don't have time to figure it out at the moment. Perhaps I will research this at a later time when I'm lessed swamped.

<--?php
$redirect = $_SERVER['HTTP_REFERER'];
session_register("redirect");
?-->

will also work if the following header is used:

<--?php header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past ?-->

0 Comments:

Post a Comment

<< Home