Sending AS3 data to php


(Nate ) #1

Hello guys! I am currently messing around with sending some AS3 data to a php file, which once I have working I would then like to send it from the php file to mysql. I followed this tutorial but changed it slightly for FlashPunk methods and I am getting a nasty error:

Here is the tutorial I followed: http://www.creativearmory.com/tutorials/how-to-use-actionscript-3-to-post-to-php-and-retrieve-variables

Here is my submit button code:

override public function update():void { if (collidePoint(x, y, world.mouseX, world.mouseY)) { if (Input.mouseReleased)click(); } }

	public function click():void
	{
		
		trace("clicking the sub button");
		
		 // We will use URLRequest to post our variables to a
		// PHP file which will return variables back

// Set the location of your PHP file
var urlReq:URLRequest = new URLRequest ("../../login.php");

// Set the method to POST
urlReq.method = URLRequestMethod.POST; 

// Define the variables to post    
var urlVars:URLVariables = new URLVariables(); 
urlVars.userName = 'myUsername';
urlVars.userPass = 'myPassword';

// Add the variables to the URLRequest
urlReq.data = urlVars;  
            
// Add the URLRequest data to a new Loader
var loader:URLLoader = new URLLoader (urlReq); 

// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoginComplete); 

// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES; 
loader.load(urlReq); 
		

	}
	
	public function onLoginComplete(event:Event): void
	{
		//retrieve success variable from our PHP
		if (event.target.data.success == "true")
		{
		trace("Login Complete"); 
		}
		
	}

As you can see, it is slightly different but still doing the same thing.

Here is my login.php file:

<?php
//Grab username and password variables
$username = $_POST['userName'];
$password = $_POST['userPass'];

// for additional variables use the &
// success=true&username=$username&password=$password
echo "success=true"; ?>

And here is the nasty error I get when I click the Submit button:

Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: file:///C|/Users/Nate/Desktop/login.php at entities::subbutton/click()[C:\Users\Nate\Desktop\database_test\src\entities\subbutton.as:71] at entities::subbutton/update()[C:\Users\Nate\Desktop\database_test\src\entities\subbutton.as:44] at net.flashpunk::World/update()[C:\Users\Nate\Desktop\database_test\src\net\flashpunk\World.as:60] at net.flashpunk::Engine/update()[C:\Users\Nate\Desktop\database_test\src\net\flashpunk\Engine.as:96] at net.flashpunk::Engine/onEnterFrame()[C:\Users\Nate\Desktop\database_test\src\net\flashpunk\Engine.as:211]

Thanks for any insight guys!


(Nate ) #2

Nevermind guys! I figured it out! I was directing the button to the location of login.php, but I guess the URLRequest was looking in the bin folder. I moved the php file to the Bin folder and it worked great!