Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Page 1 of 5 123 ... LastLast
Results 1 to 15 of 63

Thread: Sending Variables to PHP from Flash And Back again!

  1. #1

    Sending Variables to PHP from Flash

    This is part 1 of a 3 part tutorial. The parts are:

    Part 1: A simple setup that will send variables from a Flash movie to a PHP script and then that PHP script will print the variables out.

    Part 2: Sending variables from a Flash Movie to a PHP script, and then using that PHP script to send an e-mail.

    Part 3: Sending variables from Flash to a PHP script. Saving those variables to a text file, then calling them back into Flash to form a guestbook/shoutbox/tagboard within Flash.

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    Flash Player set-up
    First you need to set up the input boxes. Create an Input box by using the "Text tool" In the text properties box, you must give the input box a name. It doesn't matter what the name is, but you must remember that name because we are going to be using it in the PHP script in a few minutes.

    In my file I made three input boxes. I named them "name", "age", and "eye". Simple enough right? Ok, now we have to make the button to open up the PHP page. To open up a new page from Flash we use getURL(). The code that I used looks like this:
    PHP Code:
    submit.onPress = function () {
          
    getURL ("getVars.php""_blank""POST");

    getVars.php is the name of the PHP file that is our target.
    _blank makes the button open up a new page.
    POST is the method in which to transfer the variables. The other option is GET.

    Thats it for the Flash part.

    PHP Page set-up
    The PHP code is simple:
    PHP Code:
    <?

    // Recieving the variables.
    $name $_POST['name'];
    $age $_POST['age'];
    $eye $_POST['eye'];

    // printing out the variables
    print "Your name is "$name ".<br>";
    print 
    "You are "$age ." years old.<br>";
    print 
    "You have "$eye ." eyes.<br>";

    ?>
    Now a quick explanation of the code:

    The first three lines recieve the variables from the scripting using $_POST[]; This is necessary because the more recent versions of PHP have global variables turned off by default. On some hosts, your script will not work without using this method. You can name the variable whatever you like on the left of the equal sign ("="), but on the right, you must put the variable of your input text box.

    The second 3 lines print out the strings. The extra periods (".") in there are used to connect the strings together. Its called the concatenation operator. For more about that go here: Click Here!

    [edit]Sorry, I had to take out the script in action link because the server that the script was set up on was taken down.[/edit]

    I am going to be re-writing this as a real tutorial to be posted on Kirupa's main tutorial page. But I wrote this up while it was fresh in my mind. Hope it helps.

    Cheers,
    Jubs

  2. #2
    This is part 2 of a 3 part tutorial. The parts are:

    Part 1: A simple setup that will send variables from a Flash movie to a PHP script and then that PHP script will print the variables out.

    Part 2: Sending variables from a Flash Movie to a PHP script, and then using that PHP script to send an e-mail.

    Part 3: Sending variables from Flash to a PHP script. Saving those variables to a text file, then calling them back into Flash to form a guestbook/shoutbox/tagboard within Flash.

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    Ok, this is basically a combination of the post above, and this thread that I created the other night about using the mail() function: Click Here!

    Since most of this is already explained in those two posts, this section of the tutorial is going to be small because it serves only as a link between those two. Here we go!

    Flash Set-up

    The only difference between this file and the file before is the script for the button. You set everything up the same way. Create the input text boxes and give them a suitable name. I gave my three input boxes the names "name", "email", and "message".

    The code on the button changes a little because now, we don't want to open another page. We just want the e-mail to be sent when the user clicks the Flash button. So, we won't be using getURL() but instead we will use loadVariablesNum(). The code looks like this:

    Code:
    sendButton.onPress = function(){
           loadVariablesNum("mailTut.php", 0, "POST");
    }
    this takes the variables from level "0" and sends them to the script contained in "mailTut.php" using the "POST" method.

    PHP Set-up

    Now, if you read thru those other two posts that I showed you, then you should have no problem understanding the rest of this post. If you didn't read thru them, then you still won't have a problem because its very simple. Here is the code:

    PHP Code:
    <?

    // Recieving and Creating Variables...
    $Name $_POST['name'];
    $Email $_POST['email'];
    $Message $_POST['message'];
    $Subject "Test E-mail Tutorial";
    $Header "From: $Name <$Email>";


    //Performing Mail script...
    mail($Email$Subject$Message$Header);

    ?>
    The first 5 lines of code are "Recieving and Creating Variables". This recieves the variables from the Flash movie and then creates the Subject and Header variables to be used in the mail() script.

    The mail() script is then executed. I already explained all of this stuff in the other tutorial that can be found here: Click Here

    Ok thats its for this one.

    [edit]Sorry, I had to take out the script in action link because the server that the script was set up on was taken down.[/edit]
    Cheers,
    Jubs

  3. #3
    File Attachments

    File #1: Sending Variables to PHP from Flash

  4. #4
    File Attachments

    File #2: Using Flash and PHP to send an e-mail.

  5. #5
    Ok, re-reading my second post, I don't think I was 100% clear with what I was trying to do. My files were set up so that you could input YOUR e-mail address so you could send yourself an e-mail. And this would show you that the script worked and you could see it work. If you were to change this script around so that it would send to you from your server you would change this line:

    PHP Code:
    // change it from this:
    $Email $_POST['email'];

    // to this:
    $Email "myname@mydomain.com"
    The point of the tutorial was to give you the basics and then you could edit the script from there. Once this is posted into a larger, more formal tutorial for Kirupa's site, then it will (hopefully) be much easier to understand...

  6. #6
    178
    posts
    Registered User
    well done !

  7. #7
    outstanding!

  8. #8
    Ok here's part 3, but its not the original part 3. I'm going to make this slightly longer than originally planned, but hopefully it will be more in-depth.

    This part will show you how to send a variable to PHP, and bring it back into Flash.

    The source files are attached at the bottom of the post for those that need them.


    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    Flash Player set-up

    The Flash set up is quite simple. You need an input text-box and a dynamic text box. Give the input box a variable name of "name" (without the quotes) and the dynamic box a variable name of "read" (without the quotes).

    Create a button. I used my normal naming system and I called it "mc_button_submit_vars" You may call it whatever you wish.

    Now that the file is set up we need to add the code. Its not too terribly difficult.

    Code:
    mc_button_submit_vars.onPress = function ()
    {
    	loadVariablesNum ("kirupa_tutorial.php", 0, "POST");
    };
    As you can see we are using the same method as in the previous posts to load the variables.

    Ok, our Flash file is set up, now we want to set up the PHP page...

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    PHP File set-up

    Again, this is quite simple.

    Here is the code:

    PHP Code:
    <?
    $name 
    $_POST['name'];

    print 
    "read=Your name is " $name ".";
    ?>
    All this does is catch the variable, and then prints it out in a format that Flash can read.

    Here is a link to a working example: http://www.bluefinatlantic.com/egTes...eceive_php.htm

  9. #9
    wow jubba that was great.... is there any chance you could do a tutorial on a message board (doesnt seem a whole lot different from this, but i know it needs a mySQL database right?)...

  10. #10
    phorte's Avatar
    760
    posts
    formerly aussie devil
    thats great man!!

    but one more thing.. how would you do it so that you could create the vaiables like "post1_name" and that would be the name of the first poster in a guestbook etc.. if u get what i mean??


    and one more thing.. on the last part.. how would u then save those variables to a text file and then how would u recall them from there??


    -Scotty

  11. #11
    178
    posts
    Registered User
    hi Jubba,
    at Part 1, because I want to hide AS from viewing, I put the AS into movie, then movie, then movie (instead of first frame at root )

    Code:
    _root.submit.onPress = function () {
          getURL ("getVars.php", "_blank", "POST");
    }
    and it doesn't work
    please tell me how to hide this action if another use program like www.sothink.comto view, and why that doesn't work if I do like that, thanks

  12. #12
    Scoot: Actually, no. A message board with Flash isn't something that I want to tackle at the moment. It wouldn't be much more difficult, just much more in-depth. If you are looking for a message board that uses flash and PHP, I suggest you check out www.phpforflash.com

    Aussie: The php script would name the variables for you, and then the Flash script would catch the variables and print them out. And saving them is the next part of the tutorial and its coming up. I think I'll write that part up tonight.

    OKtrust: I'm not sure... thats more of a Flash targeting problem. Even if you put the code in a movie, in a movie, in a movie, someone, if they want it bad enough, is going to get it. If they want your code badly enough to download the SWF and have the decompiler, and then view your SWF with it, you should just let them have it because they are a lonely, desperate, sad, shell of a person. For them to know the name of your php page wouldn't really matter anyway, because they wouldn't be able to get a look at the code unless they had an FTP program and FTP access to your server....

  13. #13
    This is part 4 of a multi-part tutorial. The parts are:

    Part 1: A simple setup that will send variables from a Flash movie to a PHP script and then that PHP script will print the variables out.

    Part 2: Sending variables from a Flash Movie to a PHP script, and then using that PHP script to send an e-mail.

    Part 3: How to send a variable to PHP, and bring it back into Flash.

    Part 4: Using LoadVars(). Sending variables from Flash to a PHP script. Saving those variables to a text file, then calling them back into Flash to form a guestbook/shoutbox/tagboard within Flash.

    IMPORTANT: Now, this step gives us some options to work with. Since we are, possibly, going to be bringing large amounts of data into Flash from the PHP file we have one of two options:

    1) bring the raw data string into Flash and then use Actionscript to format it to our liking.

    2) Use PHP to format the string how we want it and then just pull it into Flash to display in the text box.

    What I am going to do, is give you the basic set-up for the files and provide the files in an attachment, then at the end of the post I will provide the code to manipulate the string.

    Be sure to read slowly because if you don't have a firm grasp on PHP and Flash string editing, you may get lost. If you don't understand this then go back and read the other tutorials before attempting this. If you still have questions then post in the Server-Side-Scripting Forum. And someone will be around to answer your questions.

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    Flash Player set-up
    First you need to set up the input and dynamic text boxes. Create an Input box by using the "Text tool" In the text properties box, you must give them instance names. In this case we are using instance names instead of variable names because we are going to be using LoadVars() and using instance names is more up-to-date syntax, as well as more powerful with the LoadVars().

    I gave the input box the instance name "sendVars" because that is the variable that we are going to be sending to the PHP script. The dynamic textbox has the instance name "recVars" because its going to be receiving the varables from the PHP script.

    In my file (which you should download to get a better feel of how its set up) I have my two text boxes (one dynamic and one input) and a button that I named subBut because its the button I'm using to submit my variables. Now you are going to want to place the loadVars actionscript in the main timeline of the movie:

    The code that I used looks like this:
    Code:
    subBut.onPress = function () {
    sendlv = new LoadVars ();
    loadlv = new LoadVars ();
    loadlv.onLoad = function (success) {
    if (success) {
    trace ("Success!");
    _root.myvar = this.myvar
    }
    else
    {
    recVars.text = "Error loading file.";
    };
    sendlv.sendVars = sendVars.text;
    sendlv.sendAndLoad ("write2text.php", loadlv, "post");
    sendVars.text = "";
    recVars.text = "";
    }
    Now lets explain the code:
    Code:
    subBut.onPress = function () {
    //This defines the MovieClip subBut 
    //as having button actions, and calls the function contained
    //in the brackets everytime the user clicks on it.
    }
    Code:
    // These lines simply define new LoadVars() objects
    sendlv = new LoadVars ();
    loadlv = new LoadVars ();
    Code:
    loadlv.onLoad = function (success) {
    if (success) {
    trace ("Success!");
    _root.myvar = this.myvar
    } else {
    recVars.text = "Error loading file.";
    }
    };
    //This function simply defines that onces the variables are loaded
    //correctly the variable from the PHP file should be pointed to the
    //dynamic text box
    Code:
    sendlv.sendVars = sendVars.text;
    //Defines the variable we are sending to the PHP script
    //as the text contained in the Input box "sendVars"
    Code:
    sendlv.sendAndLoad ("write2text.php", loadlv, "post");
    //The script that sends and loads the variables
    //"write2text.php" is our PHP file
    //loadlv is our target to load the variables
    //"post" is our method of loading the variables
    Code:
    sendVars.text = "";
    recVars.text = "";
    //these lines simply reset the text boxes to make it easier to type
    //and read
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    PHP Page set-up
    The PHP code is as follows:
    PHP Code:
    <?

    $sendVars 
    $_POST['sendVars'];

    $file "write2text.txt";  

    $fp fopen($file"a+"); 

    $data fread($fp80000); 

    ftruncate($fp0); 

    $new $sendVars ".:-:." $data

    fwrite($fp$new); 

    fclose($fp); 

    print 
    "recVars=".$new

    ?>
    Now a quick explanation of the code:
    PHP Code:
    $sendVars $_POST['sendVars']; 
    This line defines the variable $sendVars as the variable that we will be receiving from the Flash file. This is not always necessary because most server are set up differently, but it is good practice to always catch your variables this way, because your code will work no matter what server you are on.

    PHP Code:
    $file "write2text.txt"
    This line just defines a variable with the string of our text file name. It makes the code easier to read.

    PHP Code:
    $fp fopen($file"a+"); 
    This line opens the file for reading and writing and places the pointer at the beginning of the file.

    PHP Code:
    $data fread($fp80000); 
    This line reads the contents of the file and saves it to a variable $data

    PHP Code:
    ftruncate($fp0); 
    This line deletes the file contents and cuts the file size to 0. This step isn't really necessary, but this is the method that I like to use.

    PHP Code:
    $new $sendVars ".:-:." $data
    Here we define the variable $new as our Flash variable and our text file data with the string ".:-:." in between them. You will see why it is important to have something like that separating them once we get to the string manipulation later

    PHP Code:
    fwrite($fp$new); 
    This writes the variable $new into the text file

    PHP Code:
    fclose($fp); 
    Closes the text file always make sure you close the file

    PHP Code:
    print "recVars=" $new
    This prints out the variable in a form that Flash can read.

    Thats pretty much it. This should give you a way to get started. I'll have the string formatting codes later on today

    Don't forget the file attachment is below

    And a big thanks to Ahmed who helped me with the loadVars() script because I am Flash MX illiterate


    See the files in action: http://www.bluefinatlantic.com/egTes...write2text.swf

    type in the top box and click the button and you will see all the entries from the text file

    Cheers,
    Jubs

  14. #14
    very nice tut, just one question, how would i change it so that each time a new message is added it goes on a new line ?

    I tried

    $new = " $sendVars ..:-:.. <br> $data";

    and 'render text as html' was enabled
    but it dosent work

  15. #15
    thats where the string formating comes in. I haven't had time to do that yet...

    PHP Code:
    String.prototype.replaceS = function (strrepchr) {
        var 
    = (chr 0) ? this.substr(chr) : this.substr(0,chr);
        var 
    str.length;
        var 
    rep.length;
        var 
    t.indexOf(str);
        while (
    != -1){
            
    t.substr(0,p) + rep t.substr(p+s);
            
    t.indexOf(str,p+r);
        }
        return (
    chr) ? (chr 0) ? this.substr(0,this.length+chr)+t+this.substr(chr) : t;
    }


    recVars.replaceS(".:-:.""<br>"); 
    That is Senoculars prototype for replaceing a string with another string. That should get you what you want for now... This code should go in the main timeline. Put the actual prototype by itself, but put the

    PHP Code:
    recVars.replaceS(".:-:.""<br>"); 
    in the onLoad command.

Page 1 of 5 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012