Skip navigation
Arion03
Currently Being Moderated

Passing variables from flash to php script - Not working!

Feb 14, 2012 9:55 AM

Tags: #php #as2 #variables #flash_to_php

Hey everyone. I was wondering if anyone could help me. I am using as2 to pass a variable from flash to my php and nothing is being passed! Here is the action script code that I put on my button:

 

on (release)

{

          amount.text=5;

          form1= new LoadVars();

          form1.amount= amount.text;

          form1.sendAndLoad("http://mywebpage.com/test.php",amount,"POST");

          }

 

Now the amount of "5" is being inputted into a dynamic text field with the variable and instance name of amount  (which, in the future, will be hidden ). This is working fine when clicking on the button. What I am trying to do, without success, is to get this value of "5" to my test.php and have it echoed and it's just not happening . This is my php script:

 

<?php

 

$form_inp= $_POST['amount'];

if ($form_inp==''")

{

print "Nothing inputted <br>";

}

else

{

print "You have inputted: $form_inp";

}

?>

 

I'm getting "Nothing inputted" everytime.

 

I've already tried the different routes for the amount.text such as:

 

on (release)

{

  amount.text=5;

  form1= new LoadVars();

  form1.amount= this.amount.text;

  form1.sendAndLoad("http://mywebpage.com/test.php",amount,"POST");

  }

 

and

 

on (release)

{

  amount.text=5;

  form1= new LoadVars();

  form1.amount= this._parent.amount.text;

  form1.sendAndLoad("http://mywebpage.com/test.php",amount,"POST");

  }

 

etc.........

 

I've also tried changing the variable name in the sendAndLoad function from amount to form1 like this:

 

on (release)

{

  amount.text=5;

  form1= new LoadVars();

  form1.amount= amount.text;

  form1.sendAndLoad("http://mywebpage.com/test.php",form1,"POST");

  }

 

and still nothing.

 

In my php file I even tried the GET instead of the POST and it still turns up nothing.

 

<?php

 

$form_inp= $_GET['monto'];

if ($form_inp=='')

{

print "Nothing inputted <br>";

}

else

{

print "You have inputted: $form_inp";

}

?>

 

Extra info:  I have put the .swf file and the .php file on the same level in the same folder on my server just to be sure and the amount.text dynamic text field and the button are on the same level in the .swf file.

 

Can anyone shed some light on my situation.......... pretty please? I am at wits end!

 

Thank you soooo much.

 
Replies
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 14, 2012 11:18 AM   in reply to Arion03

    you shouldn't attach any code to objects and you should use a different loadvars to send and receive data:

     

     

    var sendLV:LoadVars=new LoadVars();

    var receiveLV:LoadVars=new LoadVars();

     

    yourbutton.onRelease=function

    {

              amount.text=5;

             sendLV.amount= amount.text;

              sendLV.sendAndLoad("http://mywebpage.com/test.php",receiveLV,"POST");

              }

     

    receiveLV.onData=function(src){

    trace(src);

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 16, 2012 11:51 AM   in reply to Arion03

    use:

     

    var sendLV:LoadVars=new LoadVars();

    var receiveLV:LoadVars=new LoadVars();

     

    yourbutton.onRelease=function(){

              amount.text=5;

             sendLV.amount= amount.text;

              sendLV.sendAndLoad("http://mywebpage.com/test.php",receiveLV,"POST");

    }

     

    receiveLV.onData=function(src){

    trace(src);

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 16, 2012 1:32 PM   in reply to Arion03

    now, fix your php:

     

    <?php

     

    $amount= $_POST['amount'];

    echo $amount;

     

    ?>

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 16, 2012 2:37 PM   in reply to Arion03

    what's the url to your online files?

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 17, 2012 7:52 AM   in reply to Arion03

    and what do i need to click to trigger what you think should call your php?

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 17, 2012 10:22 AM   in reply to Arion03

    change the code on that button to:

     

    var sendLV:LoadVars=new LoadVars();

    var receiveLV:LoadVars=new LoadVars();

     

    yourbutton.onRelease=function(){

              amount.text=5;

             sendLV.amount= amount.text;

    trace("sending");

              sendLV.sendAndLoad("http://mywebpage.com/test.php",receiveLV,"POST");

    }

     

    receiveLV.onData=function(src){

    trace(src);

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 17, 2012 12:23 PM   in reply to Arion03

    sending you can test in your flash program.  if you fail to see "sending" in the output panel, you're not clicking "yourbutton" after that code executes.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 17, 2012 12:51 PM   in reply to Arion03

    now, upload the swf published with that trace code.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 17, 2012 1:20 PM   in reply to Arion03

    when i click the corona bottle, i see no trace. 

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 17, 2012 2:52 PM   in reply to Arion03

    Just to make your day a little worse (sorry)..  On a Mac using Firefox the link you posted looks like this:

    (No menu, no nothing). Just the line "You have:" on a blank white page.

     

    corona.jpg

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 18, 2012 8:37 AM   in reply to Arion03

    copy the code you're using for your corona button and paste it here.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 18, 2012 9:36 AM   in reply to Arion03

    that's not the code i suggested.  use:

     

     

              var sendLV:LoadVars=new LoadVars();

              var receiveLV:LoadVars=new LoadVars();

     

    but_corona.onRelease=function()

    {     

     

              amount.text=5;

     

             sendLV.amount= amount.text;

             trace("sending");

             sendLV.sendAndLoad("http://mirador.mx/oaxaca/disco/receive_test.php",receiveLV,"POST");

    }

     

    receiveLV.onData=function(src){

    trace(src);

    }

     

    and copy and paste your php file code because i don't think you followed directions for that either.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 18, 2012 1:13 PM   in reply to Arion03

    ok, now you apparently have no textfield named amount.  use:

     

              var sendLV:LoadVars=new LoadVars();

              var receiveLV:LoadVars=new LoadVars();

     

    but_corona.onRelease=function()

    {     

     

              amount.text=5;

     

             sendLV.amount= 5;

             trace("sending");

             sendLV.sendAndLoad("receive_test.php",receiveLV,"POST");

    }

     

    receiveLV.onData=function(src){

    trace(src);

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2012 8:51 AM   in reply to Arion03

    I came across this thread looking for the same solution, though AS3 was not an option for me in this instance, but here I found a solution, so I will post a link to that thead in hope it helps some one in the future.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 24, 2012 6:43 AM   in reply to Arion03

    2012-03-24_154130.png

     

    i copied the code perfectly and i just changed the php file name. I use wamp server and register_globals are active (thats it shows). Can you also tell me how to pass variables from php to flash?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 12, 2012 2:36 AM   in reply to Arion03

    Hi guys,

    I had the same problem. I created a small maze game and I wanted to send the final time value to a php. After three hours of trying and trying and trying nothing happened. I mean the variable wasn't sent to the php. I used trace() for debugging and then at 00:30 AM i saw the bug. My AS2 was sending a variable called "timp" and the php was expecting a variable called "time". So... the mistake was actually a typo. Now everything works just fine on both my online server and on my WAMP server.

     

    Here is my AS2 code:

     

    on(press){

    var    sendVar:LoadVars = new LoadVars();

    sendVar.timp = timp;

    timp.sendAndLoad("myphp.php",timp,"POST");

    getURL ("myphp.php", "_blank", "POST");

    }

     

     

    And here is my PHP code:

     

    <?php

    // Receiving the variable.

    $time = $_POST['timp'];

     

    // printing out the variable

    print "Your time is ". $time;

     

    ?>

     

    I repeat, now everything works flawlessly. Also, you don't need to give the full path to the .php file in AS. you can just put the name of the php (if the .php and swf are in the same location).

     

    Good luck to you all and i hope this helps!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points