AS3.0+PHP写入mySQL
php中$_POST变量是一个数组,用于收集来自method="post"的值,内容是有HTTP POST方法发送的变量名称和值。
从带有POST方法的表单发送的信息,对任何人都是不可见的,并且发送的信息也没有限制。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>feiruo</title> </head> <body> <form action="welcome.php" method="post"> name: <input type="text" name="name" /> age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
<?php echo "Your name is '$_POST['name']', you are '$_POST['age']'."; ?>
$_REQUEST变量包含了$_GET,$_POST方法以及$_COOKIE内容,因此可以使用$_REQUEST变量来获取通过$_GET和$_POST方法发送的数据结果。
其实学PHP有好些时间了,但是还没有好好练习过,今天使用AS3.0调用PHP文件将数据写入到mySQL数据库。
下面是PHP文件register.php:
<?php header("Content-Type:text/html;charset=utf-8"); define('DB_USER', 'localhost'); define('DB_ROOT', 'root'); define('DB_PWD', 'eXdA8hP954ABzVD8'); define('DB_NAME', 'feiruodata'); $getName=$_POST['_name']; $getPswd=$_POST['_pswd']; //连接mySQL $con=@mysql_connect(DB_USER,DB_ROOT,DB_PWD)or die('连接错误'); //选择数据库 mysql_select_db(DB_NAME,$con)or die('Occured error'); mysql_query('SET NAMES UTF8') or die('显示错误'); $query="INSERT INTO excise01 (user_name,user_pswd) VALUES ('$getName','$getPswd')"; $row=@mysql_query($query) or die('cuowu'); //关闭数据库 mysql_close(); ?>
AS3.0文件:
package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.net.URLRequest; import flash.net.URLLoader; import flash.net.URLRequestMethod; import flash.events.Event; import flash.net.URLVariables; public class register_mc extends Sprite{ private const _URL="http://localhost/feiruo/register.php"; private var _VERIFY:_VerifyCode;//验证码 private var _Request:URLRequest; private var _Loader:URLLoader; private var _code:String=""; public function register_mc() { //设定字符区间 nickName.restrict = "a-zA-Z\u4e00-\u9fa5"; verifyCode.restrict = "a-zA-z0-9"; _VERIFY=new _VerifyCode(); this.addChild(_VERIFY); _VERIFY.x=verifyCode.x-_VERIFY.width/2; _VERIFY.y=verifyCode.y; registerBtn.addEventListener(MouseEvent.MOUSE_DOWN,registerFunc); cancelBtn.addEventListener(MouseEvent.MOUSE_DOWN,cancelFunc); this.addEventListener(Event.ENTER_FRAME, function(e:Event):void { ((nickName.text).replace(/([ ]{1})/g,"")=="" || passWord.text.length<4)?registerBtn.enabled=false:registerBtn.enabled=true; }); } private function registerFunc(e:MouseEvent):void { if(cancelBtn.hasEventListener(MouseEvent.MOUSE_DOWN)) cancelBtn.removeEventListener(MouseEvent.MOUSE_DOWN,cancelFunc); if(registerBtn.enabled) { verifyCode.text.toLowerCase()==_VERIFY.scode.toLocaleLowerCase() ? register() : verifyCode.text="错误" ; _VERIFY.refreshCode(e); cancelFunc(e); } } private function register():void { var _var:URLVariables=new URLVariables(); _Request=new URLRequest(_URL); _Request.method=URLRequestMethod.POST; _var._name=nickName.text; _var._pswd=passWord.text; _Request.data=_var; _Loader=new URLLoader(_Request); } private function cancelFunc(e:MouseEvent):void { nickName.text=""; passWord.text=""; verifyCode.text=""; } } }
AS3.0验证码:
package { /* *@ClassName:package::verifyCode * *@Intro:使用ActionScript3.0绘制常见的简单验证码 * *@Author:非若 *@E-mail:jdk.feiruo@qq.com *@Date:2015.07.12 * */ import flash.display.Sprite; import flash.display.Shape; import flash.text.TextField; import flash.text.TextFormat; public class verifyCode extends Sprite { private var tt:Sprite=new Sprite(); private var TT:Sprite=new Sprite(); private var bg:Shape; private var word:Shape; private var blurW:Shape; private var randsum:uint=(Math.random()*78)%26; public function verifyCode(arr:Array) { init_imageBG(); init_imageBlur(); init_imageW(arr); init_imageX(); init_mask(); this.addChild(tt); this.addChild(TT); TT.addChild(init_mask()); tt.mask=TT; } private function init_mask():Sprite{ var d:Sprite=new Sprite(); d.graphics.clear(); d.graphics.beginFill(setColor(),1); d.graphics.drawRect(0,0,65,32); d.graphics.endFill(); return d; } private function init_imageBG():void { bg=new Shape(); bg.graphics.clear(); bg.graphics.beginFill(0xFFFFFF,0); bg.graphics.drawRect(0,0,65,32); bg.graphics.endFill(); tt.addChild(bg); } private function init_imageW(codes:Array):void { word=new Shape(); for (var i:uint=0; i<codes.length; i++) { var t:TextField=new TextField(); var f:TextFormat=new TextFormat(); f.size = 12 * Math.random() + 12; f.color = setColor()+0x666666; f.bold=true; t.text = codes[i]; t.x = i * 16 + 1; t.selectable = false; t.y = Math.random() * 5 - 1; tt.addChild(t); t.setTextFormat(f); } } private function init_imageBlur():void { blurW=new Shape(); for (var r:uint=0; r<=3; r++) { blurW.graphics.lineStyle(3*Math.random()*1,setColor()); blurW.graphics.lineTo(65*Math.random()+5,35*Math.random()+4); blurW.graphics.moveTo(65*Math.random()-10,35*Math.random()-5); blurW.alpha=.8; tt.addChild(blurW); } } private function init_imageX():void{ init_imageW([".",".",".","."]); } private function setColor():uint { var _color:uint = 0xCCCCCC * Math.random() + 0x222200; return _color; } } }
package { /* *@ClassName:package::verifyCode * *@Intro:向verifyCode传送绘制的字符 * *@Author:非若 *@E-mail:jdk.feiruo@qq.com *@Date:2015.07.12 * */ import flash.display.Sprite; import flash.display.Shape; import flash.events.MouseEvent; public class _VerifyCode extends Sprite { public var scode:String=""; public var code:Array = []; private var words:String = "QYhg123UBNMw0erty89LKJPuXCVioplOHGFkj45fIDSAZdsaz67xcWERTvbnm"; private var vcode:verifyCode; public function _VerifyCode() { vcode=new verifyCode(getString()); addChild(vcode); vcode.buttonMode=true; this.addEventListener(MouseEvent.MOUSE_DOWN,refreshCode); } public function refreshCode(e:MouseEvent):void { removeChild(vcode); vcode=new verifyCode(getString()); addChild(vcode); } private function getString():Array { code=[]; scode=""; for (var a:uint=0; a<=3; a++) { scode+=words.charAt((Math.random()*words.length*3)%words.length); code.push(scode.charAt(a)); } return code; } } }