HI,
I am working to send data from as3 to php and I am having a little trouble with it. I am new to both AS3 and php but at this point I think I am doing fine.
The project is simple it just takes two values from as3 and send its to php. But apparently I am not able to send variables from as3 to php. Here is the code(its very simple code):
AS3 code:
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.*;
btn_mc.addEventListener(MouseEvent.MOUSE_DOWN, onLogin);
function onLogin(e:MouseEvent):void {
var username:String="aa1";
var userpass:String="aa1";
//string together a nice universal variable for php
var variables:URLVariables = new URLVariables();
variables.username = username;
variables.userpass = userpass;
trace("this is variables: " + variables);
[COLOR="Red"]** //The output is:this is variables: username=aa1&userpass=aa1**[/COLOR]
//Pass input variables to php...
var phpReq:URLRequest = new URLRequest();
phpReq.url = "http://localhost/Newfolder/insert.php";
phpReq.method = URLRequestMethod.POST;
phpReq.data = variables;
trace("this is phpReq: " + phpReq.data);
**[COLOR="DarkGreen"] //The output is:this is phpReq: username=aa1&userpass=aa1[/COLOR]**
//load the php file and the results...
var phpLoader:URLLoader = new URLLoader(phpReq);
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, onPhpLoad);
phpLoader.load(phpReq);
function onPhpLoad(e:Event):void {
var fromPhp:URLVariables = new URLVariables(e.target.data);
trace(fromPhp);
trace(fromPhp.checking);
**[COLOR="Red"]//The output is:checking=ok&foo=bar[/COLOR]**
status.text = fromPhp.checking;
[COLOR="red"]**//status.text changed to OK**[/COLOR]
}
}
insert.php
<?php
$username = $_POST[‘username’];
$userpass = $_POST[‘userpass’];
$con = mysql_connect
(“localhost”,“root”,“123”);
if (!$con)
{
die('Could not connect: ’ .
mysql_error());
}
mysql_select_db(“test”, $con);
mysql_query("INSERT INTO users
(username, password, user_bio)
VALUES ($username, $userpass, ‘35’)");
echo “foo=bar&checking=ok”;
mysql_close($con);
?>
I am sure the insert.php is working as I hardcoded the values in insert.php and it inserts the rows. So, I am assuming that there is some problem with sending the data from as3 to php. Any help will be really appreciated.
Thanks in advance