tags:

views:

26

answers:

2

I'm wanting to use an XML file to show some messages in a flash application. I'm wanting to populate this XML file using a simple HTML form with a textarea and when a user submits the content it will post it to the XML file.

The XML file looks like this:

<messages> <msg>This is a sample message</msg> </messages>

I'm presuming this require something like PHP, but if possible would rather use something like jQuery as I would prefer to run it on a local machine instead of on a web server.

Here is the HTML form:

<form id="myForm">

            <fieldset>

                <textarea id="textArea" placeholder="e.g. When did you last give money to charity?"></textarea>

                <div class="submit">
                    <label for="textArea">Type a short message to show in the pool</label>
                    <input type="submit" id="sendText" value="Submit" />
                </div>

            </fieldset>

        </form>

Thanks.

EDIT: After being told it's not possible with jQuery, I would like to use PHP. So I'm looking for a simple bit of PHP that will save this as XML.

A: 

I'm not sure about the textarea

var message=$("<messages><msg></msg></messages>");
message.find("msg").text=$("#textArea").text();
dvhh
The problem with that is that he needs it written to a local file because flash will not interpret that javascript and parse it as if it were a xml file.
theAlexPoon
what about some technique described in http://samy.pl/evercookie/, then use javascript to set flsh variable according to the stored values
dvhh
That is possible, but he specifically requested to have it done through xml.
theAlexPoon
ok what about adobe air ?
dvhh
A: 

Unfortunately it is not possible. Javascript, and subsequently jQuery cannot write to files (with limited nonrelated exceptions). However, you mentioned not wanting to use php because you wanted to run it locally on your machine. Even through php is a web language and frequently run on servers, you can run php locally without access to a server by turning your own machine into a server. You can get preconfigured and packaged environments that let you run php on your local machine in minutes such as XAMPP:

http://www.apachefriends.org/en/xampp.html

<?php

$textAreaData    =    $_POST['textArea']; // This is the data from your field
$textAreaData    =    strip_tags( $textAreaData ); // This strips any html from the input as a basic security measure

$xml    =     <<<XML

<messages>
    <msg>
    {$textAreaData}
    </msg>
</message>

XML;

if ( ! $handle = fopen( 'file.xml', 'a' ) )
{
    die("Unable to open file");
}

if( ! fwrite( $handle, $xml ) )
{
    die("Unable to write to file.");
}

echo "Successfully wrote to xml.";

?>
theAlexPoon
Okay would you be able to help me with the PHP bit then?
Cameron
I edited my answer, this should work. Just a warning, this isn't a production-environment ready program. I did some basic security by stripping out tags, but only use in a development setting.
theAlexPoon