Amfphp与Flex交互--(三 ) 向php端写入mysql数据
我们今天做的呢,前端,只要在第一章代码的基础上修改一下就好了,主要的是php端的改变。所以呢,你可以在你以前的工程里面新建一个Application,然后粘贴下面的代码哦。先看一下flex端的代码:<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF" viewSourceURL="srcview/index.html"> <mx:RemoteObject id="myservice" fault="FaultHandler(event)" showBusyCursor="true" source="hello.AddEmailDb" destination="amfphp"> <mx:method name="InputUser" result="ResultHandler(event)" /> </mx:RemoteObject> <mx:Script> <!]> </mx:Script> <!--下面这些东东呢,就是我们的显示界面了,2个按钮、一个文本区域、一个标签。 --> <mx:Button x="250" y="157" label="Send" width="79" click="myservice.getOperation('InputUser').send(result_text.text);"/> <mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('hoo').send(); "/> <mx:TextArea x="10" y="36" width="319" height="113" id="result_text"/> <mx:Label x="10" y="10" text="Result:"/> </mx:Application> 发现变化了吗?在第三行,source里面,多了一个这个东东"hello.AddEmialDb",这个是干什么的呢?就是帮助flex找到php的文件啊。在第4行,即 定义RemoteObject的里面,我们换了一个叫做InputUser的函数,当然,这个一定是php端函数啦。在第29行,按钮的click事件里面getOperation的参数也同样改变为InputUser,另外send()函数里面好像多了一个文本信息,猜猜?恩,对,这个就是要向Amfphp端发送的东东。很好理解。对吧?
然后是php端,在amfphp\services\ 新建一个hello目录,在这个目录中再新建一个叫做AddEmailDb.php的文件。然后用你所熟悉的编辑器打开它,敲入以下代码:
<?phpclass AddEmailDb {function InputUser($post) {global $host, $dbTable, $dbPass, $dbId;$host = "localhost";$dbId = "root";$dbPass = "orbit";$dbTable = "test";$link = mysql_connect ( $host, $dbId, $dbPass );mysql_select_db ( $dbTable );if (! $link) {die ( 'Connection Impossible:' . mysql_error () );} else {$sql = "Insert INTO flex(adressemail) VALUES ('$post')";mysql_query ( $sql );}mysql_close ( $link );return "success";}}?> 运行你的Apache ,开启你的mysql。在mysql里面要建立一个数据库test,
CREATE TABLE `test`.`flex` (`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,`adressemail` VARCHAR(45) NOT NULL DEFAULT '',PRIMARY KEY(`id`))ENGINE = InnoDB;在flex端运行。
看到与下面这张图一样的界面了吗?
http://dl.iteye.com/upload/attachment/502887/d071ea23-8e09-3844-b837-64c4f0801857.jpg
在文本区域内,输入一个邮件地址吧。然后点击Send按钮,如果文本框里出现了一个success的单词,那么恭喜你,成功了。
http://dl.iteye.com/upload/attachment/502889/21c10c33-b814-398b-b847-31ee0b779c19.jpg
页:
[1]