15 Replies Latest reply on Mar 16, 2007 3:23 PM by SymTsb

    loading php variables into flash 8

    grenji
      i'm trying to load some vars into flash 8. i tried loadvariables, loadvariablesnum, loadvars, google. can't figure it out.

      2 years ago when using flash mx trying the same thing, the line:
      loadVariablesNum("file",0);
      was enough to do the trick.

      but in flash 8 can't get it to work ;(

      i have the simplest php file, something like this:

      <?
      $town = "where";
      $person = "who";
      $sex = "male";

      print "town=$town&person=$person&sex=$sex";
      ?>

      the use of variables is a must, so the solution
      print "town=where&person=who&sex=male"
      doesn't do me any good. actually this works.

      so what's the correct way of loading this into flash 8?
      thanks guyz.
        • 1. Re: loading php variables into flash 8
          BorosAdam Level 1
          can you post your code? it might be helpful to figure out what's messed up... ;)

          adam
          • 2. Re: loading php variables into flash 8
            BorosAdam Level 1
            i mean your as code... :D

            adam
            • 3. Re: loading php variables into flash 8
              brad9376 Level 1
              If you need to get multiple variables from php, it may be easier to use an xml string and parse through it after it is loaded:

              Have php output something like:
              <myVars><town>Town Name</town><person>John Doe</person><sex>Male</sex></myVars>
              or
              <myVars town="Town Name" person="John Doe" sex="male" />

              In Actionscript:
              myXmlData=new XML();
              myXmlData.load(" http://.....") // your php url

              myXmlData.onLoad=function() {
              //parse xml data into variables here
              }
              • 4. Re: loading php variables into flash 8
                grenji Level 1
                brad:
                xml can be a solution. i thought about that. if i can't make without it, that's my next option.

                borosAdam:

                my code ... i tried the basic solutions.

                loadVariables("file",0);
                trace(town+" "+person+" "+sex);

                also something like this:

                a = new LoadVars();
                a.load("file");
                a.onLoad = function() {
                trace(a.town);
                trace(a.person);
                trace(a.sex);
                }

                and louds of variations.
                • 5. Re: loading php variables into flash 8
                  BorosAdam Level 1
                  look for the LoadVars class in flash help. the usage should look something like the following.

                  adam
                  • 6. loading php variables into flash 8
                    liquidleaf Level 1
                    you can also use a FlashVars= parameter in your <object><embed> code, IF you are using your PHP code to also display your flash movie, and if you only need to pass these variables once when the movie loads. If this is what you need to do, use this....
                    .....
                    <object ...>
                    ....// other params .....
                    <param name="FlashVars" value="<?
                    echo var1name;
                    ?>=<?
                    echo var1value;
                    ?>&<?
                    echo var2name;
                    ?>=<?
                    echo var2value;
                    ?>" >

                    ....
                    Though you will have to do that in two places, once in the object code, and once in the embed code.

                    The embed code would look more like
                    <embed ...... FlashVars="<? //php similar to above ?>" ..... />

                    I have used this successfully to pass in variables when the flash movie first loads, to pass in stuff like a URL for a link and other info. There are a lot of different ways to pass values in to flash. You can also use ExternalInterface.call and use Javascript along with PHP to retrieve info at any time, but that is more complex.

                    This may not be what you need - but you can still use LoadVariables with Flash 8, as well.
                    • 7. Re: loading php variables into flash 8
                      liquidleaf Level 1
                      Double checked- a project I made a few months ago uses

                      loadVariablesNum("./images/variables.dat", 0);

                      to load a file that has contents like this....

                      &intro_title=Congratulations!&
                      &intro_text=Welcome to the toolbar.&
                      &intro_note=<font color="#666666">Note:</font><br />If you do not see the toolbar at the top of your browser window:<br />Go to View > Toolbars and make sure the toolbar option is checked.&
                      &intro_end_message=We hope you enjoy using your Toolbar!&

                      I may have also had issues, I can't recall, but also had to instruct a non-Flash user how to edit the file, so the extra "&" are there as a safeguard, and in the first variable instance, to assure that the first variable was read in correctly. Extra ampersands do not hurt the code.
                      • 8. loading php variables into flash 8
                        grenji Level 1
                        liquidleaf:

                        maybe your solution is working but not for me. i have a few variables to send and i can't pass them in the object/embed tags. 10x eniuei.

                        in mx i had no problem with this. i tried also with a starting and ending & for safety.

                        the thing is that if i have the php file :

                        <?php
                        echo "&var1=2&var2=3&";
                        ?>

                        is working fine in flash, no matter what i use.... loadVars, loadVariables.

                        but i have the php file:

                        <?php
                        $var1=2;
                        $var2=3;
                        echo "&var1=$var1&var2=$var&";
                        ?>

                        this won't work. and flash will get the values "$var1" and "$var2" instead of 2 and 3.

                        and same thing with an xml file. somebody suggested me to use an xml file. so i did. if i use an xml file, and create a loadXML in flash, works fine.
                        but when echoing from php the content of the xml, doesn't work the loadXML. same problem... instead of the values i get the name of the php vars. like above.

                        maybe its a php or apache issue.
                        • 9. Re: loading php variables into flash 8
                          kglad Adobe Community Professional & MVP
                          your code in your first message was correct. why not use it?:

                          • 10. Re: loading php variables into flash 8
                            grenji Level 1
                            cause its not working.

                            this is the only thing that works. but not good enough for me. i need php vars.

                            <?php
                            echo "&var1=2&var2=3&";
                            ?>
                            • 11. Re: loading php variables into flash 8
                              SymTsb Level 2
                              your problem is the print statement.

                              <?php

                              echo("&var1=$var1&var2=$var2");

                              ?>
                              • 12. Re: loading php variables into flash 8
                                grenji Level 1
                                SymTsb:
                                that its not it ;(
                                • 13. loading php variables into flash 8
                                  SymTsb Level 2
                                  alright. what kind of testing are you doing on the data to make sure it exists? have you done any at all? If I'm using the LoadVars class, I always add a trace statement during testing to make sure I'm getting the data before I ever try to do anything with it. I apologize for going back to basics but I'm not sure what you've done or haven't done. I mentioned the echo vs print command because I have had a good deal of trouble myself using anything but echo when sending data back to flash. The code below is my simple LoadVars test script. You'll need to adjust the file location for your php file on your server. The rest should be usable as is in a blank document.

                                  var rVars:LoadVars = new LoadVars();
                                  rVars.onLoad = checkData;

                                  function checkData(){
                                  for(p in this){
                                  trace(p + " = " + this[p]);
                                  }
                                  }

                                  this.onEnterFrame = function () {
                                  rVars.load("myphp.php");
                                  delete this.onEnterFrame;
                                  }


                                  EDIT: check if this works.....

                                  <?php
                                  $var1 = 1;
                                  $var2 = 2;
                                  $var3 = 3;
                                  echo("&var1=".$var1."&var2=".$var2."&var3=".$var3);
                                  ?>
                                  • 14. Re: loading php variables into flash 8
                                    grenji Level 1
                                    SymTsb:

                                    i wasnt checking data. it seems that it takes some time to load the variables. on frame 2 i have the variables loaded correctly.

                                    <? echo "&var1=2&var2=3";?> was working fine. thats why ive never tried checking data.

                                    anyhow problem solved, thanks to all the ppl replying.
                                    • 15. Re: loading php variables into flash 8
                                      SymTsb Level 2
                                      It's important to wait for the data to load before manipulating it.

                                      Run tests to make sure the data is valid (assuming it's a db of some sort) that won't cause unexpected errors and then do something with that data from inside the onLoad event handler. This way, it's all done/verified/used in the same frame.

                                      No more padding frames are necessary.