您的位置:首页 > 编程语言 > PHP开发

AS3.0和php数据交互POST方式

2012-02-08 17:35 141 查看
AS3.0和php数据交互POST方式


AS3.0和php数据交互POST方式
首先打开flash建立一个as3.0的文件
拖 textarea和button组建到舞台上
分别给两个组件命名:txtcontent和addcontent

然后点第一帧添加动作:
var url:String = “http://localhost/tt.php”;   //执行操作数据库的php文件
var requestData:URLRequest = new URLRequest(url);   //新建URLRequest对象,用来获取flash中textArea的数据
var loader:URLLoader = new URLLoader();   //建立URLLoader对象,用来发送flash中textArea的数据
addcontent.addEventListener(MouseEvent.CLICK,addData); //为button附事件对象,点击按钮执行addData函数
function addData(e:Event){
requestData.data = String;   //   .data 为URLRequest一个属性分三种大家可以查手册查到
requestData.method = URLRequestMethod.POST;   //.method 也为 URLLoader的一个属性值
var urlvariables:URLVariables = new URLVariables(); //建立URLVariables对象,
urlvariables.cc = txtcontent.text; //通过cc参数传递 txtcontent里的数据
requestData.data = urlvariables;//讲urlvariables的数据赋值给.data
loader.load(requestData); //开始发送数据
}
php文件很简单
<?
$content = $_POST["cc"]; //获取flash传递过来的参数
$conn = mysql_connect(“localhost”,”root”,”123456″) or die(“mysql:”.mysql_error());
mysql_select_db(“guestbook”,$conn) or die(“mysql:”.mysql_error());
$sql = “insert into topic(content) values(‘$content’)”;
mysql_query($sql);
//以上为连接数据库并执行sql语句
?>


我的代码:

function sendPostJSON(array:Array):void {

var variables:URLVariables = new URLVariables();    //建立URLVariables对象
variables.name = 'HCity首都';
variables.age = '25';

/*
* 如果使用json数据的方式
var objectArray:String = '{"name":"didi","age":"28"}';
var json = JSON.decode(objectArray);
variables.name = json.name;
variables.age = json.age;
*/

var requert:URLRequest = new URLRequest('http://thinkphp3.com/addinfo.php');

requert.method = URLRequestMethod.POST;

requert.data = variables;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onURLLoaderCompleteEvent);
loader.load(requert);
}

private function onURLLoaderCompleteEvent(event:Event):void {
var data:String = URLLoader(event.target).data;
trace('onURLLoaderCompleteEvent ok:'+data);
}


php服务端的代码非常简单,就是把接收到的post数据提交数据库!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: