View Full Version : Flash & PHP to write to .txt file
I've read about similar topics in this thread, but they don't seem to help me.
I am making a quiz. I have the variables: Name, Grade, Score.
After the user takes the quiz, I want those variables to be written in a text file, so that I can evaluate their results.
Actionscript on main frame:
score = new LoadVars();
score.onLoad = function (error) {
....
}
stop();
Actionscript on a button:
on( release ) {
score.sendAndLoad("save.php", myData, "POST");
}
PHP:
<?php
$name = $_POST['Name'];
$grade = $_POST['Grade'];
$score = $_POST['Score'];
$toSave = "Name=".$name."&Grade=".$grade."&Score=".$score;
$fp = fopen("scores.txt", "w");
if(fwrite($fp, $toSave)) echo "writing=Ok";
else echo "writing=Error";
fclose($fp);
?>
What am I doing wrong, it's not working.
Thanks in advance :)
freddycodes
08-12-2004, 02:18 AM
And What is not working about it?
A couple of questions, do you want to add entries to the txt file, right now, you will just be overriding the contents.
Are you getting any result from the php script?
Try this
on( release ) {
score.send("save.php", "_blank", "POST");
}
See what comes up in the browser and report back.
Hey, thanks for replying.
I did what you said. When I click on the button, it just takes me to the PHP and the .txt file is NOT updated.
I tried it on the server, and locally.
And I don't want it to override what's on the file. I want it to add it to the end. I think I should use "a" instead of "w".
freddycodes
08-12-2004, 02:54 AM
Well I mean what do you see on the screen when it takes you to the php file?
Do you see?
writing=Ok
or
writing=Error
Yes you want to use a
$fp = fopen("scores.txt", "a");
if($fp)
{
if(fwrite($fp, $toSave."\n"))
{
print "writing=OK";
}
else
print "writing=Error writing to file";
fclose($fp);
}
else
{
print "writing=Error opening file for write";
}
Let's try that and see where the problem lies? What comes up in the browser when you use send()
mmm..pi..3.14..
08-12-2004, 07:14 AM
Here's a flash/php guestbook I made a while ago which writes text to a text file, just explore the code and figure out how it works or change how it looks to suit your needs :)
No, it doesn't say any error or success messages. I am not sure why.
mmm..pi..3.14.., thanks for the guestbook, I will check it out.
freddycodes
08-12-2004, 04:27 PM
So let's cover the basics then, you do have PHP installed on your server correct?
So when it opens the browser window if you right click and select "View Source",
What do you see?
Basically, I see the html code. Nothing is involved with php.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>save</title>
</head>
<body bgcolor="#ffffff">
<!--url's used in the movie-->
<!--text used in the movie-->
<!--
JOhn
30
9
Submited data was saved
-->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="550" height="200" id="save" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="save.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="save.swf" quality="high" bgcolor="#ffffff" width="550" height="200" name="save" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</body>
</html>
freddycodes
08-12-2004, 08:38 PM
Okay we are not on the same page here, when you use send() instead of sendAndLoad it should open a new blank window and post the data to it.
In that new blank window is where I want you to view source.
oh, ok.
<?php
$name = $_POST['Name'];
$grade = $_POST['Grade'];
$score = $_POST['Score'];
$toSave = "Name=".$name."&Grade=".$grade."&Score=".$score;
$fp = fopen("scores.txt", "w");
if(fwrite($fp, $toSave)) echo "writing=Ok";
else echo "writing=Error";
fclose($fp);
?>
freddycodes
08-12-2004, 09:09 PM
So you see that in the source through the browser?
Okay this is the part where you tell me that you are sure you have PHP installed on your server and you can use other PHP scripts on your server.
No, I tried to install PHP, and am having trouble with it.
freddycodes
08-12-2004, 09:51 PM
I am a little confused, how long did you know you didn't have PHP configured and running before you started this thread?
Ok, to make a long story short. The Quiz is going to be on a server that HAS php installed and running. I am testing it on MY server, which I suppose does not have php installed. I don't have FTP access to the other server, so I thought I could test it before it's published.
mmm..pi..3.14..
08-12-2004, 10:13 PM
Did you try my guestbook yet kp18? All you have to do is put the PHP file, Flash movie, and txt file in the same folder and it should work just fine, just make sure none of the files permissions are set to "read-only". After you remove the read-only permissions the guestbook should work just fine. I think that the guestbook is your best bet for learning how to write to a text file using PHP. By the way, the guestbook files MUST be on a server that can handle PHP in order to work, otherwise you'll get error messages left and right and have a big problem.
Let me know if anything goes wrong and I'll be glad to help :)
freddycodes
08-12-2004, 10:17 PM
Yeah your guestbook ain't gonna work without PHP. kp18, start with getting PHP running.
mmm..pi..3.14.., Yes I did check out your guestbook. Thank you very much, it showed me how to do it, better than reading books or tutorials.
I don't know what version you use, but I use MX 2004, and I think you had "loadVars();" and I had to change it to "LoadVars();". But that's no big deal, I just wanted to let you know.
I think I have figured out my problem. ----STUPID----- -WARNING: DO NOT ATTEMPT-------- I forgot to check whether I had PHP installed on my server, which I didn't. ------STUPID--------
So, hopefully if I put it on the other server, it should work.
I'll let you guys know. You guys are all really helpful.
mmm..pi..3.14..
08-12-2004, 11:41 PM
Your welcome kp18, I sent you the Flash MX version that I made because I wasn't sure what version you had, I didn't want to send you my Flash MX 2004 Pro version and have you not be able to open it :) Glad you knew how to fix it though. If you want the Flash MX pro version just ask, you can check it out on my server at http://67.166.34.5/Guestbook/guestbook.swf If you want to have it just ask and I will send you it :)
-Eric
ben swift
09-09-2004, 03:48 PM
mmm..pi... _ Hi, I just did a little test of your message board and am not getting results. it will write the email but that is all. have you run into that issue? I am running on a server with php and I've set the chmods to 777 so...?
http://nonoart.com/test/guestbook.html
ben swift
09-09-2004, 03:49 PM
oh yeah- and i get this when I compile it...
**Warning** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Case-insensitive identifier 'loadVars' will obscure built-in object 'LoadVars'.
loadVarsText = new loadVars();
**Warning** Scene=Scene 1, layer=Layer 1, frame=1:Line 15: Case-insensitive identifier 'loadVars' will obscure built-in object 'LoadVars'.
loadVarsText = new loadVars();
Total ActionScript Errors: 2 Reported Errors: 2
CyanBlue
09-09-2004, 06:50 PM
FMX 2004 curses with the case sensitive words... Use LoadVars instead of loadVars... :)
mmm..pi..3.14..
09-09-2004, 11:09 PM
Ben swift,
I've never run into that problem at all. Do you mean it's writing everything to the txt file but flash will not display the "Email" part of it? That's mind boggling, uhh...what server are you running, don't know if that makes a difference but you never know. Did you change anything in the fla file at all? If so, post it and I'll look at it.
Eric
kp18 >
Try wamp or easyphp for easy to install and configure AMP (Apache, Mysql, Php) server (window os).
C ya
Yupper3D
09-24-2004, 04:42 PM
Pi, thanks for the sample code! I was looking to see how to write/read from files. I ws able to do both! :)
Jacob Kerr
10-12-2004, 09:21 AM
Hey,
do a search on phpdev. You will find a download download it, and it installs apache server with php configured then put files in your _root folder, and you are rdy to go.
Jacob Kerr
wamp is better ...
nananannanaaaaaaaaaaaaa... :p
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.