Posted By

lorenz82 on 10/31/10


Tagged

phploader


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

sexyflash
lorenz82
donaldleegraham


PHP Loader


 / Published in: ActionScript 3
 

  1. //Usage
  2. var myPhpLoader = new phpLoader("myPhpFile.php", ["val1", "Value"], ["val2", "Value"]);
  3. myPhpLoader.addEventListener("loaded", function(e:Event) {
  4. trace(e.target.loader.data["a"]);
  5. } );
  6.  
  7. //CODE
  8. package
  9. {
  10. import flash.display.Loader;
  11. import flash.net.URLVariables;
  12. import flash.net.URLRequest;
  13. import flash.net.URLRequestMethod;
  14. import flash.net.URLLoader;
  15. import flash.net.URLLoaderDataFormat;
  16. import flash.events.Event;
  17.  
  18. public class phpLoader extends Loader
  19. {
  20. public var loader:URLLoader = new URLLoader();
  21.  
  22. public function phpLoader(url:String, ...args)
  23. {
  24. loader.addEventListener(Event.COMPLETE, Loaded);
  25. var request:URLRequest = new URLRequest(url);
  26.  
  27. if(args.length>0){
  28. var variables:URLVariables = new URLVariables();
  29. for (var i:int = 0; i < args.length; i++) {
  30. variables[args[i][0]] = args[i][1];
  31. }
  32. request.method = URLRequestMethod.POST;
  33. request.data = variables;
  34. loader.dataFormat = URLLoaderDataFormat.VARIABLES;
  35. }
  36.  
  37. try{
  38. loader.load(request);
  39. }
  40. catch (error:Error) {
  41. trace("Unable to load URL");
  42. dispatchEvent(new Event("error", true));
  43. }
  44.  
  45. function Loaded (event:Event):void {
  46. trace( loader.data );
  47. dispatchEvent(new Event("loaded", true));
  48. }
  49. }
  50.  
  51. }
  52. }
  53.  
  54. //myPhpFile.php
  55. <?PHP
  56. $val1= $_POST['val1'];
  57. $val2= $_POST['val2'];
  58. //... do something with these values.
  59. echo 'a=';
  60. echo $val1+$val2;
  61. ?>

Report this snippet  

You need to login to post a comment.