3 Replies Latest reply on May 8, 2006 12:50 PM by victor322

    writing variables to php

    victor322
      i'm experiencing a weird issue with a swf that's being used to write a number variable to a .php file, and then retrieve the value of that variable when a button is pressed. here's a link, with a basic mock-up to show you what's happening:

      http://www.unconstruct.com/storage/PHPtest4.swf

      so when the user adds value to the number using the first button, the tracker textbox logs the incrementation. at any point, the user can hit send, which shuttles the value of the variable to the php, where it's stored, and then spit out again using the print command. however, you'll notice that on the first click of the "sends value to php" button , the printed value is undefined, and it then takes two clicks thereafter to actually get the printed value updated, and sometimes it takes even more than two clicks.

      here's the actionscript on frame 1:


      var nValue:Number = new Number(0)
      var sPHPresult:Number = new Number(0)

      createEmptyMovieClip("mValueHolder", 0)

      changeValue.onPress = function(){
      nValue++
      tTracker.text = nValue
      }

      submit_vars.onPress = function (){
      loadVariables("StoresValue.php", "mValueHolder.sPHPresult")
      tResultHolder.text = mValueHolder.sPHPresult
      };


      where nValue is the number being incremented by user clicks, changeValue and submit_vars are my buttons, mValueHolder is the empty movieclip i'm storing the number in, and sPHPresult is the variable assigned to the value of that number.

      meanwhile, here's the .php:


      <?php
      $nValue = $_POST['nValue'];

      print"sPHPresult=The current value is " . $nValue . ".";
      ?>


      admittedly, i am truly a php virgin. so i have only a limited idea as to what the proper php syntax is. additionally, my experience with the loadVariables function is minimal. Now initially, i was trying to achieve this same end result with the LoadVars class, but i couldn't figure out a way to deal with the php document being written the way it is above, as opposed to the standard string that the LoadVars class expects (ie: title=Macbeth&author=Shakespeare or whatever...). At this point though, i'm wondering if i'm completely overlooking some of the fundamentals as a resulf of having glossed over several tutorials concerning flash < - > php situations, thereby confounding my end-goal. for instance, the LoadVars class seems a bit more open-ended in its application, but i attempted to modify the code to make use of it and it doesn't seem to play nice with the .php document. i can read and retrieve data from .txt files with no problem using LoadVars, but i really need the external file to be a dynamic container, which .php allows for. to my knowledge, this is the best way i can think of doing this. ultimately all i need is a solution that encapsulates a] generating a value in flash (assigned to a variable) b] storing it somewhere externally (writing the value to a server-side script), and c] retrieving it again in flash (to assign it to a new variable)

      in a nutshell, i'm fairly sure there are shortcomings to both aspects of my code. either the actionscript needs work, or the php, or both. i've looked into numerous tutorials and documentation online, and i can't quite find adequate answers. i've also scoured my manuals and texts for more write-ups on the LoadVars class. i'm not turning up anything solid.

      at any rate, i'd really appreciate any assistance you guys could give. hopefully i've been as specific as possible. even a push in the right direction would be very welcome. thanks so much
        • 1. Re: writing variables to php
          ANK...
          hi,

          i am sending you ex. ur file please put this files in your folder...
          and test it..

          From code for PHP file you have to write ur whole path...

          hope you will get this. and also it helpful to you..

          Thanks,
          Ankur

          • 2. Re: writing variables to php
            victor322 Level 1
            I replaced the code in my swf with yours above, and i substituted the path and filename for my .php document. still did not have any success in reading the value from the php file.

            are there also edits required for the php document?
            • 3. Re: writing variables to php
              victor322 Level 1
              actually, i just realized the php document on my server had been edited prior to the updated swf, so i re-uploaded the php and the two together seem to be functioning now. thanks for your help!

              though for posterity's sake, there are a couple of things that strike me as worth exploring, simply for the sake of being more knowledgeable in terms of the interaction between the LoadVars object and the .php file

              in my previous message, and as i've seen in the books i own, it's noted that the LoadVars object expects external variables to be url-encoded, and formated in name-value pairs using the "=" equals sign and separated by the "&" ampersand sign, such as [title=Macbeth&author=Shakespeare] . this is somehwhat confusing to me considering that the php document that's used doesn't follow this convention. so i'm just curious as to how flash still manages to 1st) parse the external file using php's mode of variable designation ( eg. the "$" sign), and 2nd) asign the values of the variables back to the LoadVars object as properties flash can understand.

              this may not sound like it makes much sense, but i'm just trying to wrap my head around it!

              but again, thanks for your help