View Full Version : Write to .txt file on server using php, asp, jsp
Hi. I don't know ASP, PHP or JSP, but I need to write to a simple .txt file from Flash MX by submitting details from a form within Flash.
Has anyone got a simple server-side script that I could modify? Or do you know of a tutorial that covers this?
Thanks :o)
jimburton
01-27-2003, 09:48 AM
you can do this easily with php:
<?php
$content = "Name:".$_POST["name"]." Address:".$_POST["address"];
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
?>
you need to have write access to the directory. this will overwrite the file if it exists....use fopen("myText.txt","rb") to open the file for reading if you want to add to it. you could also use the variable http_raw_post_data to dump whatever arrived in the form - it is also only available if form type is set to "text/plain" or "text/xml"....to get te form data there in the first place use loadvars - see tutorial on this site if nec.
Hi, thanks for the reply. I'm a bit of a newbie to server technology and have just discovered that the J2EE server I'm gonna b running the movies and scripts from will only work/will work better with a JSP file. Can the PHP script you wrote be modified to JSP or do you know of a JSP script that will do the same thing?
Thanks alot.
jimburton
01-27-2003, 10:29 AM
Sorry, I don't know java from a can of paint, but am positive you could do exact same thing with it, probably with equivalent methods - I googled for a jsp forum (http://www.google.co.uk/search?q=jsp+forum) though, maybe post on one of those?
Hi thanks anyway. I'll take a look on one of those forums and post any result I have here later.
Cheers.
Greencardman
01-29-2003, 06:39 PM
Sweet, this is just what i need. However, like the original poster, I don't really know anything about php or the like. So I was wondering if you could explain a little more. Does the php code you posted go in the html, or inside flash? I'm going to read up the tutorials and the like, so if you know the info is in there, then just ignore me :) But I have been looking for this for a while. Essentially, I want to have a form that someone can fill out, and it saves it as a text file. The flash movie then uses this text file from then on. This would work sort of like an update feature. The person I'm building the site for could go on the web, put his updates in the form, and flash updates automatically with the new text. I'm wondering is this would only work for that person's viewing session, or if everyone who viewed the webpage after the text file was changed would get the new text. So yeah, if this is possible let me know, and if you're feeling nice point me in the right direction. If there is a better way to do this, then if you could point the direction to that way it'd be greatly appreciated.
CyanBlue
01-29-2003, 07:02 PM
Howdy, Greencardman... :)
Just copy jimburton's script into the text editor and save the file as, say, 'writeData.php' and put it into the same directory as your Flash file is located...
When you call that PHP file from the Flash, you can use getURL() function or LoadVars() object...
jimburton's script is expecting two variables passed from Flash, name and address... So, you can go something like this...writeLVs = new LoadVars();
writeLVs.name = theName.text;
writeLVs.address= theAddress.text;
writeLVs.send("writeData.php?uniqueID=" + getTimer(), writeLVs , "POST");Try it and see if it works... :)
The update feature... I am not sure how you are approaching this but if you were to have your client update the certain texts inside of the Flash movie, you can load the text file that contains different texts... When that text file is changed by your client, everybody who loads the Flash movie after him are supposed to see the change as long as you have your scripts and text field names and such... Does this answer your question??? :)
Greencardman
01-29-2003, 09:39 PM
Hmm, I don't know if it answers my question or not. I don't think I phrased my question all that clearly last time, so I'll try it again.
Lets say I'm building a simple website for a client. The website consists of a Flash MX movie. Lets say the client is somewhat computer illiterate and doesn't have time to learn Flash, or enough money to pay me to do frequent updates. Also, his site design is going to stay the same, just one dynamic text block needs to be updated occasionally.
So therefore, I thought it would be nice if the client could go into a password protected page on his site and fill out a form. This form would then be saved as a text file on the server. Flash uses this text file as the basis for the dynamic text block. This way, all the client needs is a web browser, and he can make his updates from anywhere. This was just an idea I had, and I was wondering if it is feasible.
So yeah, hopefully that makes it a little clearer of what exactly I'm trying to do.
BTW, thanks for responding earlier CyanBlue.
CyanBlue
01-30-2003, 12:19 AM
Simple answer for your question... Yes... It can be done... Does this answer your question??? :D
Hi I've had some progress on the .jsp/.txt front, but first I'll explain a bit about what I'm trying to do:
I have a JAVA back-end that was written by a colleague which needs to send data to a flash front end every 5 seconds or so (I have got this part working, *groan* again using .txt files updated at intervals). Within the Flash front end, I want users to be able to enter data into a text field, then click a send button to send the data back to the JAVA back end which processes the data further. As I'm a newbie (in so many of these technologies), I thought that the simplest way for me to get Flash and JAVA communicating in this way would be to use .txt files and a .jsp. If you could suggest a more efficient (and simple to implement) way to do this - perhaps by passing a variable straight from Flash to the JSP to JAVA - then that would be great!
I tried out a .jsp supplied to me through jguru.com forum it looks like this:
<%
try
{
java.io.FileWriter fw = new java.io.FileWriter( "http://localhost:9080/StockMarket/param.txt" );
fw.write( request.getQueryString() );
fw.close();
out.print( "params written..." );
}
catch( Throwable t )
{
out.print( "Error " + t );
}
%>
my actionScript lookis like this:
_root.send_btn.onRelease = function(){
buyStockVars = new LoadVars();
buyStockVars.amount = input_txt.text;
buyStockVars.send("writeTo.jsp", GET)
confirm_txt.text = buyStockVars;
//confirm_txt to confirm name value pair is stored within buyStockVars
}
But, I still have problems...I have the .swf, .jsp and a file called param.txt all within the same directory. But when I run the .swf on the server nothing is written to the params.txt.
I'm don't know why it doesn't work, please HELP! ;)
CyanBlue
01-30-2003, 07:51 PM
Howdy... :)
loadVarsObject.send(url [,target, method]);
You will be either use one argument or three arguments for the send() function...
So, it has to be something like this...
buyStockVars.send("writeTo.jsp", buyStockVars, "GET");
or
buyStockVars.send("writeTo.jsp", this, "GET") ;
BTW, do you need to use GET not POST???
Lynx75
07-31-2003, 06:34 AM
okkei,
ive done what u said.
my php code is:
<?
$content = "Offerte=".$_POST["offerte"];
$fp=fopen("offerte.txt","wb");
fwrite($fp,$content);
fclose($fp);
?>
it open the file text offerte.txt,but if I do via url on a browser:
writedata.php?offerte=hihi
it write the file but it write only offerte= and nothing after.
why?
jaybee
07-31-2003, 06:44 AM
that's because your browser example has the variable in the $_GET scope not post - you can either turn register globals on in your php.ini file, so then you canjust refer to $offerte and php will do the looking for you, or preferably, check to see which scope it is in.
Lynx75
07-31-2003, 07:58 AM
okkei,ive did a few changed on the php code:
<?php
$filename = 'offerte.txt';
$somecontent = "offerte=".$_GET["offerte"];
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w'))
{print "Cannot open file ($filename)";
exit;}
// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent))
{print "Cannot write to file ($filename)";
exit;}
fclose($handle);
} else {print "The file $filename is not writable";}
?>
then i can load the external file with _root.loadVariables ("offerte.txt");
and submit with _root.loadVariables ("http://127.0.0.1/~studiomark/test/writedata2.php?offerte=" + _root.offerte_txt.text, "get");
but,and there is a BUT when I use the return it goes mad!
for each line ive did the return it use to place the offerte= in front of each!
how can I jump over this?? I wish the user can do the retutn (br) when he/she want.Non all the text in one line only without carriage returns.
bye
nick_kind
08-13-2003, 04:10 PM
Ok - I've been trying the php way to write to a file and its gone wrong!
i've put in the code as above,
on (press) {
writeLVs = new LoadVars();
writeLVs .name = theName.text;
writeLVs .address= theAddress.text;
writeLVs .send("Mail.'writeData?uniqueID=" + getTimer(), writeLVs , "POST");
}
(on a button ?)
and i get a 'The page cannot be found' message back.
However the text file does have - Name: Address:
in it.
What i need really is to just read a single number from a file and then increment it and write it back to the file?
Maybe it's the beer, but this one has really got me?
any suggestions?
cheers
NIck
couple things...
the easy one first.
with loadvars, if you want to use 'send', then the second argument should be the window you want it to load into:'_blank','_self' etc...
if you want to load it back into loadvars then the second arugment is the name of the object you want to load into (which is what you have now), but you need to use 'sendAndLoad' not 'send'.
the other thing:
your url looks realy funky. you have an apostrophe in the middle of it.
'Mail.'writeData....'
that doesn't look right. also you have no file extension... maybe that would work for php, i'm not sure, but overall you file path does look a bit odd.
nick_kind
08-14-2003, 05:39 AM
Hi there,
It's funny that you say that about the code, I did put an extension in and take out the apostraphe, and I got the same error?
Maybe I should start from scratch !!
Cheers
NIck
djspot
03-05-2005, 03:39 PM
hy to all.
I'm italian boy,very ignorant in english.
my problem is this,i have no idea for make a form to command this txt,that assing a variables to my sfw,please help me
// this line must be left at the very beginning. Important!
&imagecount= 4&
// image # 1 starts here...
&image1=image1.jpg
&imagetitle1=Quilts in the Breeze
&imagedate1=5/21/2004&
&imagetext1=&
// image # 2 starts here...
&image2=image2.jpg&
&imagetitle2=Lilacs Against A Blue Sky&
&imagedate2=5/21/2004&
&imagetext2=Some Lilacs at my parents house. .&
// image # 3 starts here...
&image3=image3.jpg&
&imagetitle3=Barns of Wisconsin&
&imagedate3=5/28/2004&
&imagetext3=Red barn not far from home.&
// image # 4 starts here...
&image4=ss1.jpg
&imagetitle4=Sunset On the Water
&imagedate4=6/15/2004&
&imagetext4=Sunset at a sinkhole nature preserve. The rest of this text is to show scrolling.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.&
&done=1&
parabolart
07-31-2005, 06:34 PM
Hi, everything works for me, but I only need to know how to make it so no windows open when you run the script?
Just like a flash chat for example...
madgett
08-02-2005, 01:43 AM
Loadvars.sendAndLoad() will not open a window as opposed to LoadVars.send() which does.
brusli
10-06-2005, 03:12 PM
HEllo I'm new in actionscripting and i have the same problem
I need a script that i can write in flash and that allows me to write a txt file with whatever i want, without using another php file or something like this. I understand that is posible to implement a javascript in flash or something like that. So pls help me just to write a .txt file directly from flash.
Thanks.
bazeem
01-28-2006, 08:39 PM
Hey, I am doing a project, and i would like to know if and how I could use C++ to save data to files in flash mx. I am usin Visual C++ .Net 2003
Thank YOu
fugitive123
01-29-2006, 07:27 AM
Don't know C++ but I believe CSharp.net works similar to VisualBasic.Net. If you're not going to make use of server script ASP or PHP, all you need to do is to add reference of "shockwave flash" into your project and make use of its methods and properties (like loadMovie & so on) with direct communication with Flash via fscommand. VisualBasic project can directly load Flash screen and communicate with it. There are few written articles about how this can be accomplished.
http://www.macromedia.com/devnet/flashslideshow/articles/part1.html
http://pages.cpsc.ucalgary.ca/~saul/vb_examples/tutorial12/
81ART
04-19-2006, 12:28 AM
ok heres the Deal
right now the site (http://www.trademarkareapartners.com/) is set up to send an email to a specific address but due to traffic thew want that data to be sent to a text file (*.csv) that can update an Excel Spreadsheet.
<?php
$content = "Name:".$_POST["Name"]." Last:".$_POST["Last"]." Email:".$_POST["Email"]." Address:".$_POST["Address"]." City:".$_POST["City"]." State:".$_POST["State"]." ZipCode:".$_POST["ZipCode"]." Phone:".$_POST["Phone"]." Fax:".$_POST["Fax"]." County_of_Interest:".$_POST["County_of_Interest"]." Zip_of_Interest:".$_POST["Zip_of_Interest"]." Occupation:".$_POST["Occupation"]." Experience:".$_POST["Experience"]." Net_Worth:".$_POST["Net_Worth"]." Liquid:".$_POST["Liquid"]." Interest_Level:".$_POST["Interest_Level"]." Comments:".$_POST["Comments"].";
$fp = fopen("nc.csv","a");
fwrite($fp,$content);
fclose($fp);
?>
and actionScript
writeLVs = new LoadVars();
writeLVs.Name = theName.text;
writeLVs.Last= theLast.text;
writeLVs.Email= theEmail.text;
writeLVs.Address= theAddress.text;
writeLVs.City= theCity.text;
writeLVs.State= theState.text;
writeLVs.ZipCode= theZipCode.text;
writeLVs.Phone= thePhone.text;
writeLVs.Fax= theFax.text;
writeLVs.County_of_Interest= theCounty_of_Interest.text;
writeLVs.Zip_of_Interest= theZip_of_Interest.text;
writeLVs.Occupation= theOccupation.text;
writeLVs.Experience= theExperience.text;
writeLVs.Net_Worth= theNet_Worth.text;
writeLVs.Liquid= theLiquid.text;
writeLVs.Interest_Level= theInterest_Level.text;
writeLVs.Comments= theComments.text;
writeLVs.send("writeDataNC.php?uniqueID=" + getTimer(), writeLVs , "POST");
where exactley do i put the actionScript? (the submit button???)
would this work???
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.