5 Replies Latest reply on Mar 14, 2008 4:33 AM by Greg Dove

    LoadVars doesn't work everytime

    akiva.kent
      On both my offline pc in using "test" withing Flash 8 and AS 2.0, and on my unix webhosting server,

      I wrote a php script that saves name-value pairs from mysql (Yeah ! Finally - if you read my other posts here!)
      &keyword10=keyword1&keyword11=organelle&keyword12=photosynthesis&keyword13=digestion&keywo rd14=&keyword15=&keyword16=

      However, both in the flash enviroment using "test" and in executing the swf after uploading to my server
      http://www.4themax.com/education/regents_quest/quiz.swf

      sometimes the LoadVars loads and decodes the name-value pairs, and other times doesn't.

      Any insight? I haven't uncovered anything in searching this forum nor in googling.

      The LoadVars AS 2.0 is (which is in the main timeline):

      traces were inserted for debugging during the AS development process.

      //sqlcache
      var my_lv:LoadVars = new LoadVars();
      my_lv.onLoad = function (success:Boolean):Void {
      if (success) {

      //Convert the variable string to properties
      my_lv.decode(my_lv.toString());
      //trace(my_lv.toString());
      // Iterate over properties in my_lv
      i=0;
      for (var prop in my_lv) {

      //trace(prop+" -> "+my_lv[prop]);
      //trace(i+" prop : "+prop);
      //trace("my_lv: "+my_lv[prop]);

      i++;
      }

      t = i-2; // used to find total array elements because the decode process populates the elements in reverse order
      //trace(t);

      } else {
      trace("Unable to load external file.");
      }
      }
      my_lv.load("sqlcache.txt");
      //sqlcache.txt

      //sqlcache repeat
      var my_lv2:LoadVars = new LoadVars();
      my_lv2.onLoad = function (success:Boolean):Void {
      if (success) {
      //Convert the variable string to properties
      my_lv2.decode(my_lv2.toString());
      //trace(my_lv.toString());
      // Iterate over properties in my_lv
      i=0;
      for (var prop in my_lv2) {
      //trace(prop+" -> "+my_lv[prop]);
      j=t-i;
      _global.keywords[j] = my_lv2[prop];
      //trace(j+" "+_global.keywords[j]);
      //trace(j+"my_lv: "+my_lv2[prop]);

      i++;
      }

      } else {
      trace("Unable to load external file.");
      }
      }
      my_lv2.load("sqlcache.txt");
      //sqlcache.txt repeat
        • 1. Re: LoadVars doesn't work everytime
          Greg Dove Level 4
          You wouldn't usually want to or need to load the same data in twice.
          I checked the link to your swf and I couldn't see it attempting to load any external data via http activity. So I don't think this code is running in your full swf (or perhaps it's only after signing in etc?)
          Also, LoadVars automatically calls its own decode method by default, so you do not need to use 'decode' in the onLoad handler.

          Here's something which might help. Hopefully the comments etc, explain a little bit. The function.apply line is probably the one that might be a little difficult to comprehend. Its only there as a quick way to append the values to the _global.keywords array (assuming that you want append them).

          • 2. LoadVars doesn't work everytime
            akiva.kent Level 1
            Dear Sir: This is quite a bit to digest and I have to enter it into and try the new code out. However, I want to thank GWD so much for taking the time and trouble to help me.

            Yours,
            Kevin
            • 3. Re: LoadVars doesn't work everytime
              Greg Dove Level 4
              You're welcome. Hopefully it will make sense when you try it.
              • 4. LoadVars doesn't work everytime
                akiva.kent Level 1
                I must say thank G-o-d, but I must also say thank you so much, GWD. It works!

                You can see it at:
                http://www.4themax.com/education/regents_quest/quiz-home.html

                (You don't need to log in to make it work. The login info is to save the testing process for educational diagnosis.)

                Note: I found that in mySQL data which becomes name-value pairs from a php script: a) spaces become + and Load Vars turns them back into spaces b) ? are not OK- I get undefined

                each array of data needs to have its own my_lv variable, but can keep prop and temparray variables the same for each array

                Thanks, thanks, thanks again!
                • 5. Re: LoadVars doesn't work everytime
                  Greg Dove Level 4
                  Again, you're welcome.

                  Using LoadVars is only one way to do this. And you should urlencode your values in your serverside script before returning them. That way you can pass "?" or "=" in the values if you need to, they will just be encoded to allow that to work properly (this is important because there will be other values like "%" that will be affected as well I believe).

                  Other ways to do it include: flash remoting,which lets you pass complex objects as well as returning complex objects, xml which lets you represent complex objects (in whatever way you choose) more easily, and even json -javascript object notation- which can also represent complex objects and is fast to encode on the server (e.g. with php 5) but is a little slower to transfer and to decode on the flash side compared with remoting/amfphp for example (see json.org for actionscript classes - I use this on my own website for pseudo-ajax style dynamic content loading).