ClarkePeter’s Weblog

September 9, 2007

Using xf:load with xforms-ready

Filed under: Xforms, php, xhtml — clarkepeters @ 2:17 pm

This is probably not interesting to anyone but me, but I’m obviously having a lot of fun finding out what I can do with xforms. As I’ve mentioned before, there might be better ways to do these things, and I wish I had the time to learn them all. So here’s a new workaround of mine.

So I have an xforms whose instance has a list of file names. I put them in an xf:select1 with xf:itemset so that the user could choose a file for viewing in the browser. Problem is, I don’t know how to do an xf:load from an instance value that is within the same xthml page as the xf:load). I know how to do an xf:load if i have an absolute url (e.g. <xf:load resource=”http://yada.com” show=”new” ev:event=”xforms-select> ) That’s what I’d like to have done, was put the load inside the xf:select1 with an “xforms-select” event and had an immediate load. I tried <xf:load ref=”//fileToLoad” … etc. but that didn’t work. I’m weak on Xpath. I’m sure there’s an xpath expression out there that would get the job done. So, in the meantime, here’s my work around.

After the user makes his selection and hits a view button it will activate the submission to a viewForm.php file (the submitting xforms is fileManager.php). So viewForm.php file will get the file name from the xml instance like so:

if (!isset($HTTP_RAW_POST_DATA))
$HTTP_RAW_POST_DATA = file_get_contents(“php://input”);
/* xmlSTR2xmlAssocARR() is my user-defined function; this function loads an xml file into an associative array. –see my Parsing without the DOM article for the code. */ $showFormAARR=xmlSTR2xmlAssocARR($HTTP_RAW_POST_DATA);
$showFormSTR=”$showFormAARR[viewFile].xhtml”;

//here I have read in the filename and added the file extension and put them into a string variable

Now that I’ve got the file name, I’ll load the form using xfload: resource=”Forms/$showFormSTR”, I can do this because my xhtml+xforms is wrapped in a php file.

But the fun part is that in the model, I’ll do an xf:load with event “xforms-ready”. In fact I’ll have two loads with event “xforms-ready”, one as a new tab/window (show=”new”), and one as a replace (show=”replace”). The new page is the target xhtml that was selected to be viewed, and the replace page is the submitting page. The net effect is that the submitting page will reload and the page-to-view will show in it’s own window/tab (however, Firefox will block the “new” page as a pop-up, so you’ll have to click “allow popup from this site” or whatever it asks you.) The user will never really know that he left the submitting page, it’ll appear as if only the new page opened. So, that’s it. Here’s the code.

<?php
header("Content-type: application/xhtml+xml");

include("fnGeneric.php");

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

$showFormAARR=xmlSTR2xmlAssocARR($HTTP_RAW_POST_DATA);
$showFormSTR="$showFormAARR[viewFile].xhtml";

$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>
   View Form
</h:title>

<xf:model>
<instance>
   <data xmlns="">
     <viewFile/>
   </data>
</instance>

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

</h:head>

<h:body>
   loading page.....
</h:body>
</h:html>
STR;

echo $page;
?>

6 Comments »

  1. Mmm…that’s some hack! :)

    The XForms load action should take values from the instance data no problem. Take a look at the XForms sample on formsPlayer that searches Amazon. The results that come back from Amazon include a URL for a page for each book, and when the user clicks on the book title the URL is used to load details about the book. The example was written for formsPlayer, but the code is pretty straightforward and should work with other XForms processors.

    All the best,

    Mark


    Mark Birbeck, formsPlayer

    mark.birbeck@formsPlayer.com | +44 (0) 20 7689 9232
    http://www.formsPlayer.com | http://internet-apps.blogspot.com

    standards. innovation.

    Comment by Mark Birbeck — September 9, 2007 @ 2:53 pm

  2. response to:
    Mark Birbeck, formsPlayer
    mark.birbeck@formsPlayer.com | +44 (0) 20 7689 9232
    http://www.formsPlayer.com | http://internet-apps.blogspot.com
    standards. innovation.

    Mark, thanks for the tip. With that encouragement I went back and changed things only I need to do a bit more (my file names originally didn’t include the full directory path so I have to go back and do that.)

    I didn’t get the event xforms-select to work inside the select1 control, which would’ve been cool. Not sure why, because when I do resource=”sampleURL” it works great but if I try to do a ref=”//xpathexpression” it doesnt’ work
    At any rate, I’ll go into all that in my next post.

    What DIDwork, though, is that I simply put my xf:load after the final select1 tag and then it worked just fine! I chose to do an action control with xforms-value-changed event instead of a trigger as was in the example you provided. So i still get that one click instant result. I’ll post this all later.
    thanks Mark

    Comment by clarkepeters — September 10, 2007 @ 6:09 am

  3. Great!

    On your XPath, try referring to the node directly, e.g., /a/b/c, rather than //c. It may be that there is a problem with the XPath parsing.

    All the best,

    Mark

    Comment by Mark Birbeck — September 10, 2007 @ 10:49 pm

  4. I had tried several different approaches such as //element, /element, ./element, ../element and so on. I’ll put the specifics in my next post. Again, I think it’s my problem really understanding Xpath and where the pointer is at any time. In this case my xf:load follows an itemset inside a select1 so maybe that’s confusing me. When I finish this project, I’m going to hit hard on some xpath tutorials and try to get myself up to snuff.

    Thanks again for keeping track. I’ll get try and get this next post up quickly. :)
    cheerio

    Comment by clarkepeters — September 11, 2007 @ 3:00 am

  5. [...] with xforms-load+= control Thanks to Mark Birbeck and his encouragement (see comments in Using xf:load with xforms-ready), I went back and reworked my previous workaround where I used an Xforms page as a redirect page [...]

    Pingback by event xforms-value-changed with xforms-load control « Clarkepeters’s Weblog — September 11, 2007 @ 6:50 am

  6. [...] to Mark Birbeck and his encouragement (see comments in Using xf:load with xforms-ready), I went back and reworked my previous workaround where I used an Xforms page as a redirect page [...]

    Pingback by ClarkePeter’s Weblog » Directory List: xforms-value-changed with xforms-load — October 21, 2008 @ 10:37 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress – Hosted by <ixwebhosting.com>