Wednesday, November 5, 2008

dealing with flex http service and passing data to PHP

Working with Flex 3 Builder for Linux


First you designate a



then add where you want your data to in php
add the URL



also I like to add in POST as your method


You have the option of a timeout value in case your backend system doesnt respond in time your app wont hang up I set mine at a 30 seconds max but depends on what your app is doing in php



I added also a Show busy cursor since I want the user to know the app is working on the deletion


now add in two more items one
  1. Faults which lets say you have an error on your php page well then you need this so flex can alert the user something is wrong.
  2. Results which tells flex what to do once its done processing the php webpage and php gave back a result
Faults on my app go like this as a parameter in HTTPService

fault="defaultFault(event)"

now flex can call a public function which tells the user a problem has occured on php's side

Results on my app can also alert the user on success
add this to your parameter
result="announcementsdeleteResult(event)"


notice my fault function called defaultFault is a general name...this is so when i have a ton of faults then can use a general function to alert the user and take action.

my result is named per the php action being taken so i named it announcementsdelete then i add on Result for sanity reading when it gets much larger.

the parameter tags must be surrounded like this


{announcements_id.text}


{announcementsgrid.selectedItem.RECORD_UPDATE_TS}



so lets break this down

I've got an announcement id which needs to be passed to the php page so i pass in to php as deleteid, im basically just naming it without having to rename my object in flex, I could of named it announcements_id if i wanted to.

{} symbols represent data which im getting from the flex object

.text is because its a text field which contains the data

so in php to get this item i do a

echo $_POST['deleteid'];

?>


I need to pass to flex back a XML response so i do this in php to tell flex im done deleting my item


$deleteid = $_POST['deleteid']

//delete the item in mysql

//return response

header('Content-type: application/xml');

if($mydelete == "success")
$results="SUCCESS";
else
$results="ERROR";

echo "
Save was $results
$results
";


?>


now flex will get a response which it can understand and parse when php is done deleting

so in flex we do this for the fault responder
public function defaultFault (event:FaultEvent):void
{
Alert.show(event.fault.message, "Error " + event.fault.faultString );

}

Now the user see's an alert if any problems with PHP occur


public function announcementsdeleteResult(event:ResultEvent):void{

if(event.target.lastResult.DATARESULTS.CODE == "ERROR")
Alert.show("Problem deleting item from mysql table please check log files");


//ok deletion worked
Alert.show("Success");

}


so the way this works is that a FAULT is when a serious error occurs like the XML is malformed or PHP has a error and throws warnings exceptions etc...

a RESULT is when php successfully returns an XML response then FLEX can parse the response and proceed doing as you wish.

heres the full code



layout="absolute" backgroundColor="white" height="100%" width="1000">




public function announcementsdeleteResult(event:ResultEvent):void{

if(event.target.lastResult.DATARESULTS.CODE == "ERROR")
Alert.show("Problem deleting item from mysql table please check log files");


//ok deletion worked
Alert.show("Success");

}

public function defaultFault (event:FaultEvent):void
{
Alert.show(event.fault.message, "Error " + event.fault.faultString );

}

]]>





{announcements_id.text}


{announcementsgrid.selectedItem.RECORD_UPDATE_TS}












No comments:

Post a Comment

Got a Suggestion please let us know