ClarkePeter’s Weblog

September 11, 2007

Using Xforms for Redirecting/Reloading pages

Filed under: Xforms, php, xhtml — clarkepeters @ 8:23 pm

In an earlier post– don’t hit the back arrow! — I was using Xforms to prevent loss of recent data from a browsers back button or browser reload page, in which case the user would be directed to an interim page that had some instructions about navigating the site and also a button where they could get back to the main menu.

I have since decided to skip the interim page and go directly to the main menu. I also did a recent post using an xforms-load control with an xforms-ready event which would send me to a new page while simultaneously reloading the current page ( see Using xf:load with xforms-ready). I’ve combined the two concepts from these articles into a single piece of code that can be used in many reload or redirect settings. For example, you may have one page that updates another, but you really don’t want to navigate away from it, but you need a reload/refresh–in this case, you’ll have the script redirect to the very page that just submitted–having a reload/refresh effect. Or, if you’ve set up a backstop which protects your data integrity in the event that someone should use the browser back page or refresh buttons, you can invoke this script directing people to the main menu or what have you.

In any case, if you’ve looked at any of my posts to see how I navigate, you’ll see how this generic page could replace a lot of what we’ve done. Here is a snippet of just one way this code might be used, in this case placed at the beginning of a file.

if (!isset($HTTP_RAW_POST_DATA))
   $HTTP_RAW_POST_DATA = file_get_contents("php://input");

$page=<<<STR
<?xml version="1.0"?>
<?xml-stylesheet href="Forms/zstyle.css" type="text/css"?>
<?xml-stylesheet href="Forms/zstylexf.css" type="text/css"?>
<?xml-stylesheet href="Forms/zstylecr.css" type="text/css"?>
<h:html
 xmlns:h="http://www.w3.org/1999/xhtml"
 xmlns:ev="http://www.w3.org/2001/xml-events"
 xmlns:xf="http://www.w3.org/2002/xforms"
 xmlns:cr="http://www.cr.com/2007/markup">

<h:head>

<h:title>
   Redirect
</h:title>

<xf:model>
   <xf:action ev:event="xforms-ready">
     <xf:load resource="index.php" show="replace"/>
   </xf:action>
</xf:model>

</h:head>

<h:body>
  redirecting page..... 
</h:body> <h:p/> redirecting page.....
</h:html>
STR;

if ( empty($HTTP_RAW_POST_DATA) )
{ echo $page; exit; }

...... continue on with the script or page display or what have you .......

Here’s another way. In this case, the script is at the end of the file, after some necessary updates have been made to an xml instance. The submitting page was fileManager.php–which is the same page we are “reloading” to:

........some process has just completed.........
$page=<<<STR
<?xml version="1.0"?>
<?xml-stylesheet href="Forms/zstyle.css" type="text/css"?>
<?xml-stylesheet href="Forms/zstylexf.css" type="text/css"?>
<?xml-stylesheet href="Forms/zstylecr.css" type="text/css"?>
<h:html
 xmlns:h="http://www.w3.org/1999/xhtml"
 xmlns:ev="http://www.w3.org/2001/xml-events"
 xmlns:xf="http://www.w3.org/2002/xforms"
 xmlns:cr="http://www.cr.com/2007/markup">

<h:head>

<h:title>
   Reload
</h:title>

<xf:model>
   <xf:action ev:event="xforms-ready">
     <xf:load resource="fileManager.php" show="replace"/>
   </xf:action>
</xf:model>

</h:head>

<h:body>
  reloading page..... 
</h:body><h:p/>  reloading page.....
</h:html>
STR;

echo $page;
 *addendum:  I forgot to mention that you'll need the
 appropriate header at the beginning of  your php file:
 <?php
 header("Content-type: application/xhtml+xml");

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress – Hosted by <ixwebhosting.com>