Send HTML Texarea content to XML File
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. 开发者_JAVA百科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.
I'm not sure about the textarea
var message=$("<messages><msg></msg></messages>");
message.find("msg").text=$("#textArea").text();
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.";
?>
精彩评论