[Xerte] Having trouble reading XML from an external script

Paul Swanson Paul.Swanson at harlandfs.com
Wed Aug 29 19:18:13 BST 2007


I was doing something similar to the PHP suggestion, but not getting
anything. Using Thomas' code, I was able to see that the data is getting
to PHP, but since it is sent by the POST method, PHP is expecting
name/value pairs, separated by ampersands. I struggled for a while in
that PHP was returning only part of the string that was sent to it by
Xerte, and realized it was breaking at an equal sign. I was using:
 
myId = 5;
myXML = "<query>SELECT * FROM tablename WHERE id = " + myId + " ORDER BY
fname</query>";
myReq = new XML(myXML);
xmlPage.sendAndLoad(myReq, 'http://localhost/test/test_xerte.php');
 
... and was receiving back only "5 ORDER BY fname". I eventually
realized that the string I was getting back was everything after the
equal sign. So I amended my string to:
 
myXML = "type=SELECT&table=tablename&id=" + myId + "&sort=fname";
 
... and now I can see all of the name/value pairs in PHP and echo them
back. I found that I needed to urlencode the strings in PHP before
echoing them, and then unescape the returning string in Xerte.
 
My PHP code is now:
 
<?php  # test_xerte.php
// this script is a test of whether I can connect to a php script
through Xerte.
 
if (!empty ($_POST)) {
    echo "<reply>";
    foreach ($_POST as $key => $value) {
            echo urlencode ("<$key>$value</$key>\n");
    }
    echo "</reply>";
} else {
    echo '<reply>The $_POST array was empty.</reply>';
}
?> 
 
In Xerte, the script that receives that data back from PHP is:
 
xmlResponse = unescape(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>";
 
So now I see all of the values returned by PHP. The xml tags don't
display, of course, but I should be able to figure out how to work with
them.
 
But since you mentioned implementing loadVars, I would like to say that
it seems that would be an easier way to communicate with PHP than using
XML. It would be similar to how PHP scripts communicate with one
another, and would make database communication easier to implement,
since you wouldn't have to convert back and forth between name/value
style and xml style.
 
- Paul



________________________________

	From: xerte-bounces at lists.nottingham.ac.uk
[mailto:xerte-bounces at lists.nottingham.ac.uk] On Behalf Of Tenney Julian
	Sent: Wednesday, August 29, 2007 5:22 AM
	To: Xerte discussion list
	Subject: RE: [Xerte] Having trouble reading XML from an external
script
	
	
	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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.nottingham.ac.uk/pipermail/xerte/attachments/20070829/8fa84fd7/attachment.html


More information about the Xerte mailing list