简体   繁体   中英

How can i send a String from AS3 to a PHP file so it can be use to do a database query

Im building this AIR app on Flash CS5, and i have this TileList that loads its data (images and names) from a database.

I want to do a query with the name any item of the Tilelist when its clicked, so i need to send the name of that item to a PHP file that will execute the query.

Im thinking of a function that loads the item name to a variable when the item is clicked.

How can i send it to the PHP file and how would i load it to the query?

Many thanks to anyone that helps.

Update

The query i want on the php file goes something like this: SELECT lat, lon FROM mapdata WHERE name= theVariable ;

You can send it via $_GET or $_POST to a php file. Get would look like:

... query.php?string={ INSERT ENCODED STRING }

You can use URLLoader to send and receive data to/from a web service. Simply add the data you wish (probably as URLVariables ). I would avoid sending whole queries like this obviously, for security reasons.

Don't forget to clean your input before putting it in a query!

I can't help much with the PHP part - it was years since I worked in PHP. You would use $_POST or $_GET, and I suspect there are ready-made functions for cleaning up data so it's safe to send it to the database (some form of escape or encode ).

As Jonatan Hedborg said:

var url:String = "http://www.[yourDomain].com/application.php";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.exampleSessionId = new Date().getTime();
variables.exampleUserLabel = "guest";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoadData);
loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFiledToLoad);
loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFiledToLoad);
loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFiledToLoad);
loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFiledToLoad);
loader.load(request);

function onLoadData(e:Event):void {
    trace("onLoadData result=" + e.target.data);
}
function onDataFiledToLoad(e:IOErrorEvent):void {
    trace("onDataFiledToLoad error=" + e.text);
}

Don't forgot to add crossdomain.xml on your server, in case for example if you are making the request from another domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM