A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: variables and php

  1. #1
    Junior Member
    Join Date
    May 2006
    Posts
    11

    variables and php

    iv been looking for a fair while now, and have read thru a fair few tutorials on it yet i jsut cant get this to work

    what i want is to simply have a text field, and when i press a button, it sends the variable onto the php script i have on a site, then when i press another button it loads the variable from the php script back to that text field or onto another text field

    i know how to save variables onto your computer, but i just cant get saving variables to a site to work

    if anyone can just tell me the php script, and the scripts to put onto the 2 buttons, that would be greatly apreciated

  2. #2
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    well...once you send the vars to PHP...it has to do SOMETHING with them...dump to a text file..or a database...(something)...
    what you CAN do is use a sendAndLoad in your varsObject..

    heres a quick run down:

    on yoru button:
    code:

    on (release) {
    _root.nameData = textField1.text;
    _root.saveText(); //function that does sendAndLoad for vars.

    }



    this code basically says: on click of this button.. I want the variable nameData (located on the _root timeline) to hold the VALUE of whatever VALUE is in the textField called textField1) and then call a function named "saveText" (which is also located on the _root timeline)


    here the saveText function that the button calls:
    code:

    function saveText() {

    // create your VAR OBJECT
    sendText = new LoadVars();
    // Format variables for PHP
    sendText.nameData = "&nameData="+nameData;
    sendText.newFileName = "text1.txt";
    //tell our script what to do WHEN/IF everything goes ok.
    sendText.onLoad = function(success) {
    if (success) {
    //response is the variableName echo'd in the PHP file.
    _root.saveResponse = sendText.response;
    }
    };
    //finally call the PHP the script
    sendText.sendAndLoad("http://www.yourDomainName.com/directoryName/saveMyText.php", sendText, POST);
    }



    now..what a sendAndLoad does..is call the php script...dump some vars to it..and immediately returns a response. for this...we just used the OK or NOT OK as the response... but you "could" use this to return the the text you want..and hold it in a VAR until you click another button to populate your second textField (although Im nto sure why you need PHP for any of this so far)

    and now the php script:
    PHP Code:
    <?
    $newFileName=$_POST["newFileName"];  //Folder HAS to have CHMOD permissions of 777

    $nameData=$_POST["nameData"];

    $fp = fopen ("directoryName/$newFileName", "w");

    if($fp)
    {  fwrite($fp,stripslashes("$nameData"));  // check to see if open was ok.
       fclose($fp);
       echo ("&response=Successfully!");
    } else {
    echo ("&response=Unsuccessfully!");
    }

    ?>
    here we PASS the VARS from FLASH to the PHP Script.. and tell PHP to save them to a textFile....and then return to FLASH either success or not success.


    now..there are MANY variations you can do with this. you could have it immediately return the text back to FLASH (instead of the succes or not success message)..

    or since PHP will be saving the text to a TEXT file...flash can DIRECTLY call the saved text file..and load the text from it.

    good luck.

  3. #3
    Junior Member
    Join Date
    May 2006
    Posts
    11
    ok, i think i got most of that

    so i also need a file on the server called text1.txt?, and do i need anyhting on that text file?

    then i jsut have that php script you gave me, and the flash right?

    also, that second code, do i jsut put that on the main timeline of the flash?

    also, in that script, do i only have to change the doamin name and it should work?

    and i also need a load button, so when i press that button, it retreavs the variable from the server and puts it back into flash textfield

    man, im still fairly confused here 8P

  4. #4
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    heres a link to an "example"
    www.dmstudios.net/demos/phpHelp/phpHelp.html

    and the source files:
    www.dmstudios.net/demos/phpHelp/phpHelp.zip

    now to address your questions:

    1.) no..the php file wil create this text file if one isnt there (if you set the permissionson the folder correctly)

    2.)just upload the HTML, .swf and the PHP file....in my included example..the php file in a sub-directory called savedData (this is also where the text file is created)

    3.) the second code (function) does go on the MAIN (_root) timeline. I used _root in the examples to make it simple..but using _root is never really a recomended practice.

    4.) yes..you need your domain name and the directory...

    example:
    (if you just load the whole folder I attached to yoru server)
    http://www.yourDomainName.com/phpHel...saveMyText.php


    or better yet to use: (once it uploaded)
    savedDate/saveMyText.php

    the PHP script needs to would need to be edited: (to your path)
    $fp = fopen ("directoryName/$newFileName", "w");
    or in my example its just: (since the php script is in same directory as text file it will create
    $fp = fopen ("$newFileName", "w");

    5.) yes..we will make another button to "load" the data from the text file off the server.... flash can read text file latently... so it wont be very hard.

    I still dont know why you need PHP (as described in your title) can you explain what it is your doing?


    I think your making the overall project too difficult...unless Im missing something?

  5. #5
    Junior Member
    Join Date
    May 2006
    Posts
    11
    well basicly iv just been looking for a way to send variables to a server so that i can then retreive those variables from like another computer

    and after a long time of looking, all the tutorials kept telling me i need to use something like php, so i assumed u needed to use php or something similair

    but if you have an eassier way that would be greatly helpfull

    i just downloaded urs, then changed the domains in the php and the flash, and uplaoded em, and i jsut get the Unsuccessfully! message

    and it should be chmod 777

    and all the permissions set to everything
    Last edited by adypete; 05-08-2006 at 03:10 AM.

  6. #6
    Junior Member
    Join Date
    May 2006
    Posts
    11
    dw, i fixed it, just had wrong url in it 8P

    anyway, thanx man, that was extremely helpfull

    much thanx

    also, you said loading was easy, but could you please just give me the code to put on a laod button

    also, i havnt tested this yet, but does it work with multi variables?
    Last edited by adypete; 05-08-2006 at 03:27 AM.

  7. #7
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    if you want another computer to (and another flash MOVIE) to read this "text file" (and whatever is in it..)...then yes you will have to use PHP....however if you want the SAME flashMovie to read it...PHP is NOT needed...

  8. #8
    Junior Member
    Join Date
    May 2006
    Posts
    11
    yeah, so i guess i do need php

    anyway, yeah, i got it working all nice and fancy, i can now save the 1 variable to the txt file, and people on other computers can read that textfile

    but what i need now is to be able to save multiple variables to the text file, i tried adding another variable, and then editing the code in the php script and the action script where i thaught i should, but i didnt get it working

    also, i still need to know how to retrieve the variables from the txt file back into flash

    srry to keep asking you these questions, but im quite eager to get this working so i can start on the acctoul flash im gonna make out of this, and you seem to be quite knowledgeable on this subject

    so if you could please jsut answer these last couple of questions it would be much apreciated

  9. #9
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    to add more variables, do this:

    actionscript part:
    code:

    sendText.newVar = "&newVar="+newVar;
    sendText.newVar1 = "&newVar1="+newVar1;
    sendText.newVar2 = "&newVar2="+newVar2;



    for the PHP, do this:
    PHP Code:
    $nameData=$_POST["nameData"];
    if(
    $fp)
    {  
    fwrite($fp,stripslashes("$nameData\n$newVar\n$newVar1\n$newVar2")); 
    then of course on your BUTTON (or whatever) you need to supply those VARIABLES with some data/values
    code:

    on (release) {
    _root.nameData = textField1.text;
    _root.newVar = textField2.text;
    _root.newVar1 = textField3.text;
    _root.newVar2 = textField4.text;
    _root.saveText(); //function that does sendAndLoad for vars.
    }




    also..I dont understand this:
    and people on other computers can read that textfile
    then you ask how to read the textFile???

  10. #10
    Junior Member
    Join Date
    May 2006
    Posts
    11
    well it saves the variables onto the text file and other people can read that textfile

    but what i need is to load the variables back into the flash to read the variables form the flash

  11. #11
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    how are people reading the text file on the server if NOT loaded back into FLASH (or something already)

  12. #12
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    anyways..to make/use a varObject:

    code:

    this.onPress = function() {
    myVars = new LoadVars();
    myVars.onLoad = function (success) {
    if (success) {
    _root.textBox1.html = true;
    _root.textBox1.htmlText = (myVars.nameData); //nameData = variable in text file
    } else {
    _root.textBox1.htmlText = "Error loading text file";
    }
    }
    myVars.load("myTextFile.txt");
    }



    (I'm using a movieClip as button here...you can change it to on(press) if you like.)

  13. #13
    Junior Member
    Join Date
    May 2006
    Posts
    11
    thanx man,

    i think i finaly got everything i need for this flash i want to make

    much apreciated

    and they where reading the messages straight from the text file
    like i gave them the url for the txt file

    but yeah, got the variables comingback into flash now

  14. #14
    Member
    Join Date
    Jul 2006
    Posts
    49
    Nevermind...lol

  15. #15
    Senior Member
    Join Date
    Aug 2004
    Location
    London
    Posts
    211
    what about posting an array or object ? is this possible ?
    ----------------------------
    A travel blog

    Landed At
    ----------------------------

  16. #16
    Senior Member
    Join Date
    Aug 2004
    Location
    London
    Posts
    211
    I think that you can separate values with a comma ok and then split on the coma thus

    &varArray=1,e,5,ff,7,8&var2=normalVar
    ----------------------------
    A travel blog

    Landed At
    ----------------------------

  17. #17
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    you can totally import a flashVar that has values separated by (whatever)..

    this is STILL one variable though..until you import it into Flash..and split based o n your delimiting character.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center