Skip to main content
Inspiring
February 14, 2012
Answered

Passing variables from flash to php script - Not working!

  • February 14, 2012
  • 3 replies
  • 17813 views

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.

This topic has been closed for replies.
Correct answer 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);

}


Ok. So as not to leave this thread unanswered, after a looooooong haul of trial and error, I switched to as3 and started doing some testing. As it turned out the register_globals was set to off on my site and that's why I wasn't getting the variable passed with the $_POST in my php. So what I finally came up with is the following:

In my fla/swf doc:

import flash.net.URLVariables;

import flash.net.URLRequest;

import flash.net.sendToURL;

var urlVariables:URLVariables = new URLVariables;

urlVariables.amount = "5";

var urlRequest:URLRequest = new URLRequest("http://mirador.mx/oaxaca/disco/receive_test.php");

urlRequest.data = urlVariables;

sendToURL(urlRequest);

In my php file:

$amount = $_GET["amount"];

echo  " AMOUNT IS:" . $amount;

Now for all of you that have been following this thread or that may read it in the future, I know that we started it off with AS2 and finishing it here with AS3 but I hadn't realized why it wasn't working until I got so fed up that I changed to AS3 to see if I could figure it out. I, like many others I imagine, felt a bit intimidated with AS3 and was reluctant to give it a try but I highly recommend you delve into it because once you get a grasp on it it's alot more functional.

So I´m going to mark this thread as answered and hope that it saves someone else all the time and frustration that it caused me.... Happy programming!


3 replies

Participant
September 12, 2012

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!

Known Participant
March 24, 2012

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?

kglad
Community Expert
Community Expert
February 14, 2012

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);

}

Arion03Author
Inspiring
February 16, 2012

Ok. I took a day off in order to come back with a clear head and give this a try. This is what happens:

When I put the code above in my actionscript it throws back an error message saying that a function name is expected. So I gave it a name and wrote it like this:

var sendLV:LoadVars=new LoadVars();

var receiveLV:LoadVars=new LoadVars();

myButton.onRelease=function

(pass)

{        

          sendLV.amount= amount.text;

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

          }

receiveLV.onData=function(src){

trace(src);

}

With my php like this:

<?php

$receiveLV= $_POST['monto'];

if ($receiveLV==''")

{

print "Nothing inputted <br>";

}

else

{

print "You have inputted: $receiveLV";

}

?>

But nothing comes back. Then I thought that since I gave the function the name of "pass" that perhaps I should put it in the parenthesis on the bottom, like this:

var sendLV:LoadVars=new LoadVars();

var receiveLV:LoadVars=new LoadVars();

myButton.onRelease=function

(pass)

{        

          sendLV.amount= amount.text;

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

          }

receiveLV.onData=function(pass){

trace(pass);

}

Yet once again it gives me that nothing was inputted.

Now I REALLY don't know what's wrong. Any ideas? Thanks in advance for your help .

kglad
Community Expert
Community Expert
February 16, 2012

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);

}