jump to navigation

Passing and Receving PHP Variables – Flex3 July 3, 2010

Posted by Tournas Dimitrios in Flex and PHP.
trackback

The following article covers the particular topic of passing and receiving php variables within Flex 3 using both MXML and AS3 (Actionscript 3.0). Not many articles out their clearly explain this particular topic with clarity; therefore ive taken the liberty of explaining the code involved and including sample files that everyone can use to easily create dynamic web apps.

Please be aware that basic knowledge of mxml and as3 is needed in order to understand what is going on. Also keep in mind that you need a “local” or live webserver in order to test this application. I will be covering such topics in the future and hope that you enjoy this tutorial .

CREATING THE RESULT FUNCTION

<mx:script>
		<!--[CDATA[
			import mx.rpc.events.ResultEvent;
			import mx.controls.Alert;

			//ResultEvent for HTTPService
			private function LoginResult(e:ResultEvent):void{
				Alert.show(e.result.email as String, 'Message From PHP');
			}
		]]-->
</mx:script>

CREATING THE HTTPSERVICE

<!--
	httpservice used to communicate with php
	*make sure the resultformat is in flashvars
	*make sure the variables in php receive the POST not GET
	*the location of the php file is essential, let it be
		the absolute path to the php file called.
-->
<mx:httpservice id="loginService" url="http://localhost/Retrieving_PHP_Variables/PHP/test.php" method="POST" resultformat="flashvars" result="LoginResult(event)">
	<!-- variables in php that will receive information -->
	<mx:request xmlns="">
		<email>
			<!-- retrieve the text input of the email formitem below -->
			{email.text}
		</email>
	</mx:request>
</mx:httpservice>

CREATING THE FORM

<!-- canvas used to hold from -->
<mx:canvas width="100%" height="100%">
	<!-- form for text input -->
	<mx:form horizontalcenter="0" verticalcenter="0" width="100%" height="100%">
		<mx:formheading label="Login">
		<mx:formitem label="email">
		    <mx:textinput id="email">
		</mx:textinput></mx:formitem>
		<mx:formitem label="password">
		    <mx:textinput displayaspassword="true" id="password">
		</mx:textinput></mx:formitem>
	</mx:formheading></mx:form>
	<!-- activate the httpservice when clicked -->
	<mx:button id="submitBTN" label="login" right="30" bottom="10" click="{loginService.send()}">
</mx:button></mx:canvas>

NOW FOR SOME PHP

< --?php-- 
	//email variable must receive the same exact variable in flash
	//this means that casing is sensitive and must be exact
	/*
		ex.
		FLASH:
			Variable : username
		PHP:
			$_POST['username']
	*/
	$email = $_POST['email'];

	//must echo out a query type string for flash to parse variables
	echo "email=$email";
? >


And thats all you need!

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s