Question

Forgive me if this has already been answered/ is extremely basic/ the question is worded incorrectly, I am very new to this and struggling.

Basically I have back end PHP which generates XML, the flash builder then inherits the data. Where I'm stuck is understanding how the flash builder can send a parameter to the PHP through an HttpService e.g

This is what it currently interprets:

http://..../file.php?action=getitems

What I would like the flash builder to send is

&class=fruit (<- the class would be dependant on what is selected from the drop down in the application)

to overall create this string

http://..../file.php?action=getitems&class=fruit

Thank you and apologies if this is nonsense. I'm using Flash Builder 4.

Was it helpful?

Solution

This is actually rather simple in Flex...

var service : HTTPService = new HTTPService();
service.url = "http://localhost/getData.php";
service.method = "POST";


var parameters:Object = new Object();
parameters["action"] = "getitems";
parameters["class"] = "fruit";

service.send(parameters);

... done!

OTHER TIPS

Overall I would use the push method instead of passing a variable, lessens the chance of getting hacked from the middle.

My AS3 Code for the http call:

    public function someRequest() : void
    {
        var service : HTTPService = new HTTPService();
        service.url = "http://localhost/getData.php";
        service.useProxy = false;
        service.method = "POST";
        service.contentType = "application/xml";    // Pass XML data.
        service.request = "<ID>somevalue</ID>";     // The XML data.
        service.resultFormat = "xml";               // Recieve XML data.
        service.addEventListener(ResultEvent.RESULT, createFields);
        service.addEventListener(FaultEvent.FAULT, handleFault);
        service.send();
    }

    private function createFields(event : ResultEvent) : void
    {
        var result : String = event.result.toString();
        returnData = XML(result);
    }

    private function handleFault(event : FaultEvent) : void
    {
        var faultstring : String = event.fault.faultString;
        Alert.show(faultstring);
    }

As you see toward the middle, there is an XML space for entering a variable. I use this approach to pass data back and forth from the PHP to the AS3.

The PHP is:

<?php

define("DATABASE_SERVER", "localhost");
define("DATABASE_USERNAME", "root");
define("DATABASE_PASSWORD", "**");
define("DATABASE_NAME", "dbName");

//connect to the database.
$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);

mysql_select_db(DATABASE_NAME);

$Query  = "SELECT * from data WHERE employeeID = '" . ($_POST['ID']) . "'";
$Result = mysql_query($Query);

$Return = "<data>";

while ($User = mysql_fetch_object($Result))
{
    $Return .= "<user><userid>" . $User->userid . "</userid><username>" . $User->username . "</username><emailaddress>" . $User->emailaddress . "</emailaddress></user>";
}
$Return .= "</data>";
mysql_free_result($Result);
print ($Return)
?>

Hope that helps you on your way.

I generally handle this through [POST] instead of [GET]

In your actionscript function:

private function sendRequest():void {
var obj:Object = new Object();
obj.action="getitems";
obj.class="fruit";

myService.send(obj);

Your httpService

<s:HTTPService id='myService' url='urlToYourPHP' method='POST' result='yourResultHandler' fault='yourFaultHandler' resultFormat='XML'/>

As powelljf3 said, POST is more secure then GET though it can still be gotten to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top