28 Replies Latest reply on Sep 8, 2007 2:51 PM by SW_Fuze

    LoadVars and PHP

    SW_Fuze
      I'm trying to use LoadVars to bring a user ID into Flash from a PHP page and then put the user ID into a dynamic text box. The user ID is created from 2 separate numbers that are received via a form. The problem is that I can't get Flash to load the variable. If I hardcode a number in then everything works fine, but it Flash doesn't seem to like dynamically created variables.

      php code that works:
      <?php echo "&qID=54564656&"; ?>

      php code that doesn't work. With this one the Flash text field is blank. It doesn't even read "undefined".
      <?php
      $subDB = substr($_POST["quizDB"], 4);
      $qID = $subDB.$_POST["quizNum"];
      echo "&qID=".$qID."&";
      ?>

      Flash code:
      fromQuiz = new LoadVars();
      fromQuiz.load("test.php");

      fromQuiz.onLoad = function()
      {
      ID_txt.text = fromQuiz.qID;
      }


      Thanks
        • 1. Re: LoadVars and PHP
          clbeech Level 3
          No this should work fine with Flash, but you should call the right variable in the onload. in the echo you have a variable named '&qID' which is what Flash is looking for, and in the onLoad you have just 'qID'

          change to:

          ID_txt.text = fromQuiz.&qID;
          • 2. Re: LoadVars and PHP
            Greg Dove Level 4
            There are no POST variables. Probably should be more like this (not syntax checked).

            fromQuiz = new LoadVars();
            toQuiz = new LoadVars();
            fromQuiz.quizBD="SOMETHING";
            fromQuiz.quizNum=500;
            fromQuiz.sendAndLoad("test.php",toQuiz,"POST");

            toQuiz.onLoad = function()
            {
            ID_txt.text = this.qID;
            }
            • 3. Re: LoadVars and PHP
              SW_Fuze Level 1
              Flash insists that I have the first "&" in the string or it won't work.
              • 4. Re: LoadVars and PHP
                Greg Dove Level 4
                @clbeech - I don't think that will make a difference - I think the first ampersand is treated the same as the subsequent name/value delimiteres.. not 100% sure... but based on the fact that the hard-coded value was working... I don't think that will change things.
                • 5. Re: LoadVars and PHP
                  clbeech Level 3
                  Yeah for sure GWD, I was saying he was asking for the wrong variable name on the AS return side, I was just assuming he was sending something also, duh, I should have seen that :) good one.

                  @SW_Fuse: no that's not true, it can be any name, not starting with a number.
                  • 6. Re: LoadVars and PHP
                    SW_Fuze Level 1
                    It will also work this way:

                    <?php

                    $qID = "5454545";
                    echo "&qID=".$qID."&";

                    ?>
                    • 7. LoadVars and PHP
                      Greg Dove Level 4
                      clbeech: No - you're also wrong about the variable name (if I understood your point correctly). The first ampersand is ignored and does not form part of the variable name.
                      Check this out:
                      var myLV:LoadVars = new LoadVars();
                      myLV.decode("&aa=helloworld");
                      trace(myLV.aa)
                      • 8. Re: LoadVars and PHP
                        SW_Fuze Level 1
                        GWD: Sorry, but I don't understand your solution using the sendAndLoad()
                        • 9. Re: LoadVars and PHP
                          Greg Dove Level 4
                          @SW_Fuze: You're trying to access some POST variables. They don't exist unless you send them to the server.

                          • 10. Re: LoadVars and PHP
                            Greg Dove Level 4
                            where you have

                            $subDB = substr($_POST["quizDB"], 4);

                            You need to have sent 'quizDB' as a value by the post method in order for the server to have access to that value in the script that its executing
                            • 11. Re: LoadVars and PHP
                              clbeech Level 3
                              @GWD: wow for sure man! I just assumed that it wasn't a corresponding variable, interesting, I love it, learn something new every day ;)

                              Yet still, Flash doesn't require this format right?

                              @SW: you need to send a value to be used with your $_POST in the PHP then the new value will be returned to the second LoadVar.
                              • 12. Re: LoadVars and PHP
                                SW_Fuze Level 1
                                They do have value (unless I don't understand what your saying). I can echo the $_POST['quizDB'] straight to the screen.
                                • 13. Re: LoadVars and PHP
                                  clbeech Level 3
                                  huh, you must be sending this somewhere else then?
                                  • 14. Re: LoadVars and PHP
                                    Greg Dove Level 4
                                    @clbeech: no flash doesn't require this format, lol

                                    @SW : then I'm confused how its getting the value - I am not a PHP expert. But the $_POST global array is only populated by values sent by the post method. I guess it might be possible to assign them in php code... but that seems unusual to me if its possible.
                                    • 15. Re: LoadVars and PHP
                                      SW_Fuze Level 1
                                      there is a form that is posting all of the information. Sorry about not mentioning that critical information, but the $_POST variable(s) do have a value.
                                      • 16. Re: LoadVars and PHP
                                        Greg Dove Level 4
                                        @clbeech: You'll have to take over here, I think your PHP knowledge is ahead of mine. I always thought POST variables only existed for the individual request/response and not beyond that.... but I'm out of my depth.
                                        • 17. Re: LoadVars and PHP
                                          SW_Fuze Level 1
                                          What I've found out then is that Flash only loads hardcoded variables. Has anyone else found this to be true?
                                          • 18. Re: LoadVars and PHP
                                            clbeech Level 3
                                            @GWD: I don't think I've got a big knowledge base than you man :)

                                            @SW: no not quite, you can send any variable, the funny thing is here that if you can echo a value from the POST then 'when' does this happen, after submiting the form? that would make sense, but the php script 'hanging onto' a varible that was previously entered via a POST at another time, doesn't seem to make sense. The value should only 'live' while the script is being executed, unless it's 'running' somehow (I have no idea how that would be possible, but it probably could be done) But in this case, I think the reason your not getting a return value on loading, is due to just that, the POST happens at a different time, than when you're asking for the return. You need to do them synchronously, that is when you POST the form info, return the varible at that time and place it into a var for local storage. this is why GWD specified using the 'sendAndLoad' method. You would do this when you pass the POST variable(s).

                                            Now you've used the var name 'quizDB', usually the DB refers to a database, if you are storing info from the user in a database like MySQL, you could use the loading php to acces the DB and then return a ID value, that's possible, without using sendAndLoad. But if your trying to save the POST info for a later time (like when recalling the ID, why not use a local var or a SharedObject to track it by user that way. a local var of course will only save the info durring the life of the swf, however you can store info on the users computer with a SharedObject that would can be recalled, anytime the user visits, kind of like a cookie.

                                            Just my take on it :) but if you want to use php to process something and get a return, you just need to use sendAndLoad, and do all of the crunching at that time. And for that, I don't understand why you wouldn't do it within Flash, so I can only conclude that you must be storing the info, for a later time.
                                            • 19. Re: LoadVars and PHP
                                              SW_Fuze Level 1
                                              cbeech: Thanks for the help. I'm more designer than developer, so some of the things you say don't sink in right away. The variables are coming from a form on another page. The form posts to quiz.php where the $_POST[] variables are. The Flash movie is on the same page (quiz.php). Yes I am storing the info for later use, so I can send information from Flash into a database. The reason for the qID is so that right record is updated, but I'm probably giving you more information than you need.

                                              I just don't understand if I can echo the variable to the screen in the correct format, why can't Flash see it. I've also tried it using a $_GET[] variable, and I have the same result; NADA.
                                              • 20. Re: LoadVars and PHP
                                                Greg Dove Level 4
                                                @clbeech: My impression -right or wrong - is that you are more familiar with php/mysql than I am. It wouldn't be difficult to be so. Either way its reassuring to see that you have the same 'understanding' of the POST variables.

                                                @SW_Fuze: Flash being in the same php page doesn't make a difference here because its flash in the browser that is making the request. I think the key here is that flash is making a separate 'request' to the form and that 'get' and 'post' variables only exist for the duration of the script executing the request. Perhaps you can store things in php session variables or something if necessary. Also... although your question is related to flash... this is an actionscript forum, not a php one. That doesn't mean that you won't get php help here. Just that it might not be the best place for it or the core knowledge of the people responding... which would definitely include me. So I'm going to bow out here in case my php advice is wrong. Best of luck with it.
                                                • 21. Re: LoadVars and PHP
                                                  SW_Fuze Level 1
                                                  I believe the limitation/problem is the ActionScript; not the PHP. I've tried storing the variables into a session format, but the result is the same. Looks like I'll have to find a work-around.
                                                  • 22. Re: LoadVars and PHP
                                                    Greg Dove Level 4
                                                    I can answer that one. The problem is definitely not the actionscript. It may be - as clbeech pointed out earlier - a question of how you're trying to do whatever you are trying to do.
                                                    I've done things with POST variables before in PHP and if you use the sendAndLoad it should work as a demonstraton. I'll confirm it shortly by testing what I posted earlier.
                                                    Basically sendAndLoad sends the POST variables to PHP. PHP has access to them - the POST variables - and executes the script and returns a response. After that script is executed... it is my belief that those variables no longer exist. I think a form works the same way. If the response opens in a new browser window echoing your POST variables from the form then perhaps they no longer exist after that point which is why flash can't retreive them on a subsequent attempt. I may be completely wrong but I will test it in the next hour or so.
                                                    • 23. Re: LoadVars and PHP
                                                      Greg Dove Level 4
                                                      OK. So I tested it.

                                                      I believe, based on my testing, that what clbeech I both said was correct. My sendAndLoad example correctly sends the POST variables to be returned, but they're not available on a subsequent request. That's just how php works and should be no different for example from two different form submissions from a html page I think.

                                                      I don't know enough about php to suggest a way to save that - I assumed you could save values in a session variable but that is something I need to learn how to do as well. But its also unclear why you're doing things the way you are and whether you need to do them the way you seem to be. As clbeech already pointed out earlier on. Perhaps if you explain what you need to do (html form / flash etc included) we can advise a better approach.
                                                      • 24. Re: LoadVars and PHP
                                                        clbeech Level 3
                                                        @GWD - I appreciate it, but I'd like everyone to know that my php knowledge is very basic, I'm certainly not an advanced user. I try to learn a little more all the time, but I probably have no business giving advice to anyone about it. yet the basic rules of programming apply, the syntax and method structure are just a little different. And you're right on target with the this problem as far as I know, I think that this is the case, and that the variable won't have a persistent value once posted and then called for at a later time on the same script, unless store by another means.

                                                        @SW: the php session var, like the others, will only be 'alive' while the script is executing, thus the same result. After the 'form' POSTs to the quiz.php what are you doing with the posted info? Are you putting it in a DB at that point? If so then you could query the DB from Flash, through php to return the quiz/ID number(s), this would work.

                                                        If your not entering the form info into a DB at the time of the POST, then another option might be to pass the variables directly to Flash via a FlashVar, rather than through a php script, then when ready to store the users info, you could send the works to php to insert into the DB. Just another option :)
                                                        • 25. Re: LoadVars and PHP
                                                          SW_Fuze Level 1
                                                          GWD: Here's the logic behind what I'm doing, but I'm definitely open to suggestions.

                                                          The user fills out a form with name, phone, etc. on the form page. The users then submits the form to the quiz page where a unique user ID is created (from multiple POST variables sent from the form), and the form values and ID are inserted into the database for that user. On the same page is a Flash quiz which will be separated into mulitple sections. After each section a Pass or Fail will be inserted into the database for that section. The reason I want to pass the user ID into Flash is so that I can send the user ID and the Pass or Fail from Flash to the PHP script to update their db record.

                                                          Hopefully that makes sense, whether there's a better way to do it - probably.

                                                          Thanks
                                                          • 26. Re: LoadVars and PHP
                                                            Greg Dove Level 4
                                                            OK... well the post variables are available when the quiz page is returned to the browser. So, like clbeech said, you could just include them as flash vars in the page so they are immediately available to flash. Otherwise flash has to make another request to the server to obtain them - which seems unnecessary - the server would presumably then have to get them out of the database and return them. But I think (again I'm out of my depth with the server side stuff) that php also gives you a way to keep data like this in session variables which in turn links to a unique PHP sessionID so that you can be sure of each separate 'user'.

                                                            How are you embedding the flash? Do you use swfobject.js or the activecontent template from adobe? Are you familiar with flashvars etc. If so, generate the html/inline javascript that is relevant to your swf, same as normal in the quiz.php script, but include some flashvars for the variables you want flash to know about and give them the $_POST['formvariable'] values. That way the form data goes straight to flash as soon as its loaded.

                                                            • 27. Re: LoadVars and PHP
                                                              Greg Dove Level 4
                                                              Or you could do your form in say frame 1 in flash and have it submit the data and goto another frame for the quiz (without a html page refresh) and have the quiz there as well. Flash already knows the data because it is also 'the form'.
                                                              • 28. Re: LoadVars and PHP
                                                                SW_Fuze Level 1
                                                                I'm not familiar with flashvars.

                                                                I will be using swfobject, but for right now I just inserted it with Dreamweaver.