Replacing html forms with flex2

After learning a bit about flex I decided to see where I could use it straight away.
One area I thought it would be useful is to replace our complex ajax forms with nice flex 2 forms.

Here I ran into a slight problem.

Flex 2 allows you to build nice apps but its not immediately clear how to submit the data to a web page like a normal html form.
This is because Flex is not really meant to act like an html form, its meant for asynchronous data transfer, like ajax, so there is really no ’submission’,
just the use of web services and
other technologies to immediately update data.

Perhaps I was trying to use the but I wanted to use flex for something, perhaps just to justify taking the time to learn it.

So below is the code I came up with for getting a flex app to submit like a regular html form.

complete code

 

public function submitToBrowser():void {
var url:String = “http://path to submit page”;
var u:URLRequest = new URLRequest();
var variables:URLVariables = new URLVariables();

variables.name = userName.text;

u.data = variables;
u.url = url;
u.method = “GET”;
navigateToURL(u,”_self”);
}
]]>



What the function does is create a url variable to point to the webpage which will process the data. For testing I just used a cfm page that dumped out the data from the cgi.
It creates a URLVariables object which holds all our form data, and then it creates a URLRequest object which will hold all the info for the form submission.

 var url:String = "http://path to submit page";
var u:URLRequest = new URLRequest();
var variables:URLVariables = new URLVariables();

Then we add the userName to the URLVariables object and then we assign this to the URLRequests data variable.

 variables.name = userName.text;
u.data = variables;

We set the url and method cariable for the URLRequest object

 u.url = url;
u.method = “GET”;

And finally we use the function navigateToURL to submit the form.

navigateToURL(u,"_self"); 

Now this may not be the best practise way to achieve this but it works for me. If you know of a simpler way please comment and let us know.

Comments
Add New
Powered by !JoomlaComment 3.26

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 

Help my beer fund

RocketTheme Joomla Templates