[Xerte] Having trouble reading XML from an external script
Tenney Julian
Julian.Tenney at nottingham.ac.uk
Wed Aug 29 13:22:15 BST 2007
Hi,
Xerte does do the sendAndLoad a little differently as in Xerte
sendAndLoad is a method of an icon; in Flash it is a method of an xml
object. I did it this way because you need to listen for the event and
it is easy to set up listeners to events broadcast by icons in Xerte,
rather than objects.
I can implement a loadVars in similar fasion if you want?
To get at the string POSTed into the php page, try this:
$empty = $post = array();
foreach ($_POST as $varname => $varvalue) {
if (empty($varvalue)) {
$empty[$varname] = $varvalue;
} else {
$post[$varname] = $varvalue;
}
}
[Thanks to Thomas]
Julian
________________________________
From: xerte-bounces at lists.nottingham.ac.uk
[mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Paul Swanson
Sent: 28 August 2007 21:38
To: xerte at lists.nottingham.ac.uk
Subject: RE: [Xerte] Having trouble reading XML from an external
script
Thanks Julian.
I'm getting close (I think) to sorting this out. I am able to
retreive data sent from an external PHP script, but I'm having trouble
figuring out how to read what is sent to the PHP script. According to an
ActionScript 1.0 reference I found, sendAndLoad uses the POST method for
sending data, but I haven't been able to access any POST variables
coming in. Interestingly, I have a block of code in my PHP script that
indicates the $_POST array is not empty , but print_r $_POST prints
nothing.
Is there something different in Xerte's implementation of
sendAndLoad? The reference I have indicates the URL is the first
argument for the method, with the reply object 2nd, yet your example
code has the reply object first and the url second.
Also, is there an implementation of LoadVars that could be used?
I'm more used to name/value pairs and that might be easier than using
XML format.
My Xerte sending script:
myId = 5;
myReq = new XML();
myReq.query = "<query>SELECT * FROM tablename WHERE id = " +
myId + " ORDER BY fname</query>";
xmlPage.sendAndLoad(myReq,
'http://localhost/test/test_xerte.php');
My PHP script:
<?php # test_xerte.php
// this script is a test of whether I can connect to a php
script through Xerte.
ob_start();
if (!empty ($_POST)) {
echo "<reply>";
echo "There is something coming in:<br />";
print_r ($_POST);
echo "</reply>";
} else {
echo '<reply>The $_POST array was empty.</reply>';
}
ob_end_flush();
?>
My Xerte receiving script:
xmlResponse = xmlPage.templateData.reply;
replyPage.myText = '<p><b>Response from script: </b>';
replyPage.myText += '<i>' + xmlResponse + '</i></p>';
replyPage.myText += "<br /><p>That's all folks!</p><br
/><p>Another paragraph.</p>";
This prints:
Response from script: There is something coming in:
That's all folks!
Another paragraph.
I think I just need that last bit of knowledge that allows me to
parse the string that Xerte sends to PHP.
- Paul
________________________________
From: xerte-bounces at lists.nottingham.ac.uk
[mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of
xerte at lists.nottingham.ac.uk
Sent: Tuesday, August 28, 2007 1:38 AM
To: xerte at lists.nottingham.ac.uk
Subject: RE: [Xerte] Having trouble reading XML from an
external script
Close. Loading XML is asynchronous, so the call to load
it isn't returned straight away. You need to wait for it. I've put a
simple example at
www.nottingham.ac.uk/xerte/examples/loadXML.zip
- Create a new file
- Give the first page the id 'xmlPage'
- add a script to it:
myReq = new XML('<request/>');
xmlPage.sendAndLoad(myReq, FileLocation + 'data.xml');
- Add an interaction ot the page
- Add an event response to the interaction: eventName:
'onSendAndLoad', icon: 'xmlPage'
- Add a script icon to the event response:
debug(xmlPage.serverXML);
debug(xmlPage.templateData.data);
The first object you can use is the raw xml. Do what you
lkike with it. To save a lot of processing the xml, it is also available
as an object. This object follows the structure of the xml file, but you
can drill down through the hierarchy using dot notation. So, if you have
an xml file like this:
<question>
<prompt>Here is a prompt</prompt>
<option>
<text>True</text>
<feedback>You clicked option true</feedback>
</option>
<option>
<text>false</text>
<feedback>You clicked option false</feedback>
</option>
</question>
You access the values of the nodes and attributes using
iconID.templateData.question[0].prompt[0]; //returns
'here is a prompt'
or
iconID.templateData.option[1].text[0]; //returns 'true'
You can also construct the flowline based on the
contents of the xml file using the iconID.duplicate function. This means
you can easily create dynamic templates that build themselves at runtime
using the contents of the xml. This is a powerful approach and I use it
all the time. I'm going to document this more fully / write a manual, as
it is fairly advanced, both conceptually and technically.
Julian
________________________________
From: xerte-bounces at lists.nottingham.ac.uk
[mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of
xerte at lists.nottingham.ac.uk
Sent: 24 August 2007 17:41
To: xerte at lists.nottingham.ac.uk
Subject: [Xerte] Having trouble reading XML from
an external script
Using Xerte 1.7
I am attempting to display some XML returned
from an external PHP script, and not having any success. Here is my
script icon contents:
========
var xml_msg:String = "<params fname=\"Fred\"
lname=\"Smith\" />";
var my_xml:XML = new XML(xml_msg);
var myReply:XML = new XML();
//reply.myText = "Supposed to be the reply from
my PHP script";
//myReply.ignorewhite = true;
my_xml.sendAndLoad('http://localhost/test/test_xerte.php', myReply);
reply.myText =
myReply.firstChild.attributes.value;
=========
My PHP script isn't doing anything with the data
sent to it, it just returns some XML:
=========
<?php # test_xerte.php
// this script is a test of whether I can
connect to a php script through Xerte.
echo '<?xml version="1.0" ?>
<reply value="Hello, World!" />';
?>
=========
I've also tried it without the '<?xml
version="1.0" ?>' declaration in my PHP script. In Xerte, I have a Text
Display icon (id=reply) with {myText} embedded. It displays "undefined"
with the above code. If I comment out the last two lines in the Xerte
script icon, and uncomment the reply.myText = "Supposed to be the reply
from my PHP script" that is what prints in Xerte.
I also get "undefined" if I paste '<reply
value="Hello, World!" />' into var myReply:XML = new XML('<reply
value="Hello, World!" />') and comment out the sendAndLoad line, so I
have a feeling this problem has nothing to do with the PHP script.
Any suggestions are greatly appreciated.
_____________________________
Paul Swanson
Instructional Designer
Harland Financial Solutions
_____________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.nottingham.ac.uk/pipermail/xerte/attachments/20070829/743b9bba/attachment.html
More information about the Xerte
mailing list