7 Replies Latest reply on Jan 6, 2011 8:23 AM by kglad

    High Score Table: Writing a Simple Text File with Flash and PHP

    Kwangle Level 1

      I am having a problem getting Flash to work with PHP as I need Flash to read and write to a text file on a server to store simple name/score data for a games hi score table. I can read from the text file into Flash easily enough but also need to write to the file when a new high score is reached, so I need to use PHP to do that. I can send the data from flash to the php file via POST but so far it is not working. The PHP file is confirmed as working as I added an echo to the file which displayed a message so I  could check that the server was running PHP - the files were also uploaded to a remote server so I  could test them properly. Flash code is as follows:

       

      //php filewriter

      var myLV = new LoadVars();

      function sendData() {

      //sets up variable 'hsdata' to send to php

      myLV.hsdata = myText;

      myLV.send("hiscores.php");

      }

       

      I believe this sends the variable 'myText' to the php file as a variable called 'hsdata' which I want the php file to write into a text file. The mytext variable is just a long string that has all the scores and names in the hiscore. OK, XML would be better way of doing this but for speed I just want to get basic functionality working, so storing a simple text sting is adequate for now. The PHP code that reads the Flash 'hsdata' variable and writes it to the text file 'scores.txt' follows:

       

      <?php

      //assigns to variable the data POSTed from flash

      $flashdata = $_POST["hsdata"];

      //file handler opens file and erases all contents with w arg

      $fh = fopen("scores.txt","w");

      //adds data to file

      fwrite ($fh,$flashdata);

      //closes file

      fclose ($fh);

      echo 'php file is working';

      ?>

       

      Any help with this would be greatly appreciated - once I can get php to write simple text files I should be ok. Thanks.

        • 1. Re: High Score Table: Writing a Simple Text File with Flash and PHP
          kglad Adobe Community Professional & MVP

          you're missing a parameter in your send method.

           

          but rather than just fix that you really shoud use two loadvars instances and the sendAndLoad() method.  it's more reliable:

           

          var sendLV:LoadVars=new LoadVars();

          var receiveLV:LoadVars=new LoadVars();

           

          receiveLV.onData=function(src){

          //the onData method is helpful for debugging

          //even if you don't need any return.  php error

          // messages will be in src

          }

          function sendData(){

          sendLV.hasdata=myTF.text;  // you should use the text property of your textfields, not their variable property

          sendLV.sendAndLoad("hiscores.php",receiveLV,"POST");

          }

          1 person found this helpful
          • 2. Re: High Score Table: Writing a Simple Text File with Flash and PHP
            Kwangle Level 1

            Thanks for that help.

             

            I've updated the code and am now using sendAndLoad which seems to work ok but am struggling with the php side of things. How do I set up a variable in php and send it back to flash? Does a variable sent from php have to have an '&' sign preceeding it? How do I send the varaible to flash in php code? I think I can use 'echo'  or 'print' but am having problems getting this working - php syntax seems a bit strange. The php file is deffo communicating with the flash file as the saveAndLoad reports success but I just cant get the php file to send useful data back to flash.

             

            Any ideas?

            • 3. Re: High Score Table: Writing a Simple Text File with Flash and PHP
              kglad Adobe Community Professional & MVP

              if you use the onData method you can echo whatever you want back to flash:

               

              // in php

               

              $var1 = "whatever";

              echo "this is the return string plus a php variable value " . $vari1;

               

               

              receiveLV.onData=function(src){

              var yourflashvar:String = src;

              }

               

              // if you want to return a handful of variable/values from php, you should use the onLoad() method.

              • 4. Re: High Score Table: Writing a Simple Text File with Flash and PHP
                Kwangle Level 1

                Thanks for your help.

                 

                I have got Flash working to a certain extent with PHP using loadVars but have been unable to get flash to receive a variable declared in PHP. Here's my Flash code:

                 

                var outLV = new LoadVars();

                var inLV = new LoadVars();

                function sendData() {

                outLV.hsdata = "Hello from Flash";

                outLV.sendAndLoad("http://www.mysite.com/hiscores/test23.php",inLV,"post");

                }

                inLV.onLoad = function(success) {

                if (success) {

                  //sets dynamic text box to show variable sent from php

                  statusTxt.text = phpmess;

                } else {

                  statusTxt.text = "No Data Received";

                }

                };

                 

                This works ok and the inLV.onLoad function reports that it is receiving data but does not display the variable received from PHP. The PHP file is like this:

                 

                <?php

                $mytxt =$_POST['hsdata'];

                $myfile = "test23.txt";

                $fh = fopen($myfile,'w');

                //adds data to file

                fwrite($fh, $mytxt);

                //closes file

                fclose ($fh);

                $mess = "hello there from php";

                echo ("&phpmess=$mess&");

                ?>

                 

                The PHP file is correctly receiving the hsdata from flash and writing it to a text file, but there seems to be a problem with the final part of the code which is intended to send a variable called 'phpmess' back to Flash, this is the string "hello there from php". How do I set up Flash and PHP so that PHP can send a variable back to Flash using echo? Really have tried everything but am totally baffled. Online tutorials have given numerous different syntax configurations for how the PHP file should be written which has really confused me - any help would be greatly appreciated.

                • 5. Re: High Score Table: Writing a Simple Text File with Flash and PHP
                  kglad Adobe Community Professional & MVP

                  var outLV = new LoadVars();

                   

                  var inLV = new LoadVars();

                  function sendData() {

                  outLV.hsdata = "Hello from Flash";

                  outLV.sendAndLoad("http://www.mysite.com/hiscores/test23.php",inLV,"POST");

                  }

                  inLV.onLoad = function(success) {

                  if (success) {

                    //sets dynamic text box to show variable sent from php

                    statusTxt.text = this.phpmess;

                  } else {

                    statusTxt.text = "No Data Received";

                  }

                  };

                   

                  This works ok and the inLV.onLoad function reports that it is receiving data but does not display the variable received from PHP. The PHP file is like this:

                   

                   

                  <?php

                  $mytxt =$_POST['hsdata'];

                  $myfile = "test23.txt";

                  $fh = fopen($myfile,'w');

                  //adds data to file

                  fwrite($fh, $mytxt);

                  //closes file

                  fclose ($fh);

                  $mess = "hello there from php";

                  echo "phpmess=".$mess;

                  ?>

                   

                   

                  • 6. Re: High Score Table: Writing a Simple Text File with Flash and PHP
                    Kwangle Level 1

                    Thanks very much for that - it works perfectly.

                     

                    I'm still really confused about the syntax of PHP so echo sends variables to Flash and have seen different versions that are claimed to work but didn't. Some more general guidance for PHP and Flash would be very welcome.

                     

                    I notice that you specifically highlighted that POST had to be in capitals but have also seen a number of scripts where it was in lower case - is this a known issue that affects functionality in Flash? However I had other problems with my code so perhaps that was the issue and not the echo statement.

                     

                    Thanks again.

                    • 7. Re: High Score Table: Writing a Simple Text File with Flash and PHP
                      kglad Adobe Community Professional & MVP

                      i think they'll both work but you should use upper case because that's correct syntax and it's a good habit to use correct syntax even when you can get away with being sloppy.