Hi!
I have an menu for user to populate it. Then when the user finish, I would like to store his Order into an XML database. I do not know how to write to XML file in Flash. Please help.
Thanks
Hi!
I have an menu for user to populate it. Then when the user finish, I would like to store his Order into an XML database. I do not know how to write to XML file in Flash. Please help.
Thanks
I’m a lazy man, so I’m just going to copy and paste a post I made at another forum many moons ago…
It’s late here and I’m pressed for time, so not much explanation…
First though - this ONLY works in PHP 5 (that’s why I asked)… I’m just waiting for (mt) to get out of the php 4.3 stone age, so I can use this myself…
Anyway… first make a quickie xml file to use named “quotes.xml”… just real simple like:
XML:
[AS]<?xml version=“1.0”?>
<webQuotes>
<item>
<author>Sammy Snail</author>
<quote>I am not a snail.</quote>
</item>
</webQuotes>[/AS]
Then your php (“addQuote.php”) is gonna look like this… hope it makes sense… I’ll try to go into more depth later if not…
PHP code:
[AS]<?php
$subName = $_POST['subName'];
$subQuote = $_POST['subQuote'];
$dom = new DomDocument();
$dom->load("quotes.xml");
$item = $dom->createElement("item");
$author = $dom->createElement("author");
$authorText = $dom->createTextNode($subName);
$author->appendChild($authorText);
$quote = $dom->createElement("quote");
$quoteText = $dom->createTextNode($subQuote);
$quote->appendChild($quoteText);
$item->appendChild($author);
$item->appendChild($quote);
$dom->documentElement->appendChild($item);
if ($dom->save("quotes.xml")){
echo "result=success";
}else{
echo "resut=failure";
}
?> [/AS]
then in Flash you’ll have something like this… this script assumes that on the stage you have a “submit_mc” to press when you enter a quote… a “get_mc” to get a random quote… Two input text boxes, “name_txt” and “quote_txt” for submitting a quote and “output_txt” to display a random quote…
[AS]stop();
//
// declare some variables…
var submitVars:LoadVars = new LoadVars();
var returnVars:LoadVars = new LoadVars();
var quoteXml:XML = new XML();
var authorArray:Array = new Array();
var quoteArray:Array = new Array();
//
get_mc.enabled = false;
submit_mc.enabled = false;
//
//
returnVars.onLoad = function(success) {
if (success) {
if (this.result=“success”) {
loadXml();
} else {
trace(“something’s jacked up, jack”);
}
} else {
trace(“no return from php.”);
}
};
//
quoteXml.ignoreWhite = true;
quoteXml.onLoad = function(success) {
if (success) {
var allNodes:Array = this.firstChild.childNodes;
var len:Number = allNodes.length;
for (var i = 0; i<len; i++) {
authorArray.push(allNodes*.firstChild.firstChild);
quoteArray.push(allNodes*.firstChild.nextSibling.firstChild);
}
submit_mc.enabled = true;
get_mc.enabled = true;
} else {
trace(“xml.noLoad();”);
}
submit_mc.enabled = true;
get_mc.enabled = true;
name_txt.text = “”;
quote_txt.text = “”;
};
//
function loadXml():Void {
// for local testing
quoteXml.load(“quotes.xml”);
// for live on server…
//quoteXml.load(“quotes.xml?”+Math.round(Math.random()*99999));
}
//
function getRandomQuote():String {
var rand:Number = Math.floor(Math.random()*authorArray.length);
var author:String = authorArray[rand].toString();
var quote:String = quoteArray[rand].toString();
return author+" said, "“+quote+”"";
}
//
submit_mc.onRelease = function() {
if (name_txt.text == “” || name_txt.text == undefined || quote_txt.text == “” || quote_txt.text == undefined) {
return;
} else {
this.enabled = false;
this._parent.get_mc.enabled = false;
submitVars.subName = name_txt.text;
submitVars.subQuote = quote_txt.text;
// for local testing
submitVars.sendAndLoad(“http://localhost/www/xmlWrite/addQuote.php”, returnVars, “POST”);
// for live server use
//submitVars.sendAndLoad(“addQuote.php”, returnVars, “POST”);
}
};
get_mc.onRelease = function() {
output_txt.text = getRandomQuote();
};
loadXml();[/AS]
hope that makes some sense… off to bug mediatemple to update to php5…
d.
EDIT:
This script will do the trick in PHP 4.3…
PHP code:
[AS]<?php
$subName = $_POST['subName'];
$subQuote = $_POST['subQuote'];
$dom = domxml_open_file("quotes.xml");
$root = $dom->document_element();
$item = $dom->create_element("item");
$author = $dom->create_element("author");
$authorText = $dom->create_text_node($subName);
$author->append_child($authorText);
$quote = $dom->create_element("quote");
$quoteText = $dom->create_text_node($subQuote);
$quote->append_child($quoteText);
$item->append_child($author);
$item->append_child($quote);
$root->append_child($item);
if ($dom->dump_file("quotes.xml", false, true)){
echo "result=success";
}else{
echo "resut=failure";
}
?> [/AS]
The result:
http://www.onebyonedesign.com/betaTest/quotes/
But with very little work, this could turn into a shout box, or something like this http://www.vodafonemayfly.co.uk/
easy peasy…
Thank you so much for your answer. I appreciate it very much. I infact have heard about using php for that purpose but it seems that this is for some kind of web based solution, where server can run php. I am doing a pure stand alone Flash demo and looking for thw way to write xml using just Flash. But now it seems that Flash is so related to the web :pleased:
unfortuantly flash is mainly a web based program and it itself cannot perform actions such as writing to a file, it needs a backend system such as PHP or ASP to achieve this, sorry i cant help further.
this is pretty cool… but I can’t get it to work on my local machine. I kept getting an error:
Error opening URL "http://localhost/www/xmlWrite/addQuote.php"
no return from php.
so I changed the path to just “addQuote.php”, but then when I submit the field values, nothing happens… the fields just go blank and the data hasn’t been written to the xml file. any ideas why I can’t get mine to work? thanks
hey, woodman…
you have to be sure you have php installed an running on your local machine to test it locally. That and make sure the path to your addQuote.php file is correct… “http://localhost/www/xmlWrite/addQuote.php” just happened to be where I had mine. When you put it online (on a server that has php installed) you can use a relative path with (hopefully) no problem…
:: Copyright KIRUPA 2024 //--