ActionScript error when attempting call method within PHP file - php

I have been following a flash tutorial online and I have created a simple flash interface. I am trying to retrieve data from my SQL database via a PHP file and display. I get the following error when I compile:
Error #1009: Cannot access a property or method of a null object reference
var variables1:URLVariables = new URLVariables();
var varSend1:URLRequest = new URLRequest("databaseCall.php");
varSend1.method = URLRequestMethod.POST;
varSend1.data = variables1;
var varLoader1:URLLoader=new URLLoader();
varLoader1.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader1.addEventListener(Event.COMPLETE,completeHandler1);
variables1.comType = "requestEntries";
varLoader1.load(varSend1);
function completeHandler1(event:Event):void{
if(event.target.data.returnBody ==""){
gbOutput_txt.text = "No data coming through";
} else{
gbOutput_txt.condenseWhite = true;
gbOutput_txt.htmlText = "" +event.target.data.returnBody;
}
}
My code exactly matches the code that is used within the tutorial. I have modified the php file to simply return "" so the issue almost definitely lies within the action script...I think :S The compiler falls over when he completeHandler1 function is called. What do you think could be causing this?
Thanks in advance.

You need to declare the completeHandler1 function before you attempt to use it anywhere else.
Here is your code modified to describe what I am talking about.
var variables1:URLVariables = new URLVariables();
var varSend1:URLRequest = new URLRequest("databaseCall.php");
varSend1.method = URLRequestMethod.POST;
varSend1.data = variables1;
function completeHandler1(event:Event):void{
if(event.target.data.returnBody ==""){
gbOutput_txt.text = "No data coming through";
} else{
gbOutput_txt.condenseWhite = true;
gbOutput_txt.htmlText = "" +event.target.data.returnBody;
}
}
var varLoader1:URLLoader=new URLLoader();
varLoader1.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader1.addEventListener(Event.COMPLETE,completeHandler1);
variables1.comType = "requestEntries";
varLoader1.load(varSend1);
Give this a try and let me know if it works.

Related

Uploading An Image As A ByteArray to a Blob using AS3 & AMFPHP

I'm currently having a bit of an issue when I try to upload a photo taking using an iOS camera to a MySQL database using PHP and unfortunately have been unable to find the right help online.
Basically The User takes a photo on their iOS device and I take that raw MediaPromise and put it into a ByteArray. I then call a PHP function using AMFPHP to add the binary to a Blob in my database. But when I test the whole thing, it never seems to work. Could somebody maybe help me with this problem or at least point me in the right direction? It would be highly appreciated. Here's the code:
AS3:
var dataSource:IDataInput;
function imageSelected1(event:MediaEvent) {
var imagePromise:MediaPromise = event.data;
dataSource = imagePromise.open();
if( imagePromise.isAsync ) {
var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, onDataComplete );
}
else {
readMediaData();
}
}
// PHP Connection //
// Database //
var gw2:NetConnection = new NetConnection();
gw2.connect(connectiongoeshere);
// Responder //
var pictureresponder:Responder = new Responder(onpicture);
function onpicture(pictureobj:Object) {
gotoAndStop(1);
}
function onDataComplete( event:Event ):void {
readMediaData();
}
function readMediaData() {
var imageBytes:ByteArray = new ByteArray();
//dataSource.readBytes( imageBytes );
dataSource.readBytes(imageBytes);
gw2.call("class.setData", pictureresponder, imageBytes);
}
PHP:
function setData($ba) {
// Connecting to the database //
mysql_pconnect("hi", "hi", "hi");
mysql_select_db("hi");
$result = mysql_query("UPDATE users set profilepicture2 ='$ba->data' WHERE email= 'email#gmail.com'");
return $result;
}

ActionScript 3.0 Button Compile error

I want to create a login database in Flash via MySQL PHP route. I copied a large portion of the code from some tutorials. My login basically contains users entering their email address picking a password and I have a basic Combobox.
When I run the code I receive this error...
ReferenceError: Error #1069: Property data not found fl.controls.Button and there is no default value.
at phpRegister_fla::MainTimeline/btnHandler()
I have debugged Flash but I don't get any additional information.
After searching online I still don't understand what is causing the error.
I hope my code will help you pinpoint where I am going wrong. Apologies its kind of long.
Any help much appreciated.
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.text.TextField;
import fl.data.DataProvider;
import fl.controls.Button;
btn_Submit.addEventListener(MouseEvent.CLICK, btnHandler);
//Validate form fields
function btnHandler(event:MouseEvent):void {
status_Txt.text = "" + event.target.data.systemResult;
trace(event.target.data.systemResult);
var phpVars:URLVariables = new URLVariables();
var phpFileRequest:URLRequest = new URLRequest("phpFile");
phpFileRequest.method = URLRequestMethod.POST;
phpFileRequest.data = phpVars;
phpVars.email = email.text;
phpVars.ps_wd = ps_wd.text;
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
}
textOneField.addEventListener(Event.CHANGE, changeHandler);
function changeHandler(event:Event):void {
trace("data entered");
}
textTwoField.addEventListener(Event.CHANGE, changeData);
function changeData(event:Event):void {
trace("data changed");
}
email.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
ps_wd.addEventListener(KeyboardEvent.KEY_UP, keyEnter);
function keyHandler(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.ENTER)
trace("keyboard was pressed");
}
function keyEnter(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.ENTER)
trace("Enter button hit");
}
var persons:Array = new Array();
persons[0] = "Male";
persons[1] = "Female";
c_two.dataProvider = new DataProvider(persons);
c_two.addEventListener(Event.CHANGE, dataHandler);
function dataHandler(event:Event):void {
trace(event.target.value);
}
There probably isn't anything wrong with the code. It is rather that you are using a component that doesn't exist in the "standard flash library" namely fl.controls.button. In order for you to be able to use that you need to add a linkage to that component.
Since you didn't mention how you compile your code it is kinda hard to tell you what to do. However, you probably don't need the fl-button but could do with a "SimpleButton" or "Movieclip" or something else instead.
If Flash:
http://forums.adobe.com/message/4260710?tstart=0
Similar issue:
AS3 Error: '1172: Definition fl.controls:Button could not be found.'
More info:
http://www.actionscript-flash-guru.com/blog/14-flcontrols-not-found-how-do-i-import-the-fl-package
In your btnHandler function you have :
... event.target.data.systemResult ...
Where event.target seem to be an fl.control.Button object.
Those objects have no "data" property.
i don't know what you are looking for in event.target.data.systemResult ?

UrlRequest not working inside a SimpleButton

I have a UrlRequest in my Main Class, that does a request to a server in cakePHP, and its working fine, but when I do the exact same request in a click of a button its not working.
Can someone help me?
This is my code to do the request:
var token:String = LoaderInfo(root.loaderInfo).parameters.requestToken;
var myData:URLRequest = new URLRequest("users/personalInfo/"+token);
myData.method = URLRequestMethod.GET;
loader = new URLLoader(myData);
loader.addEventListener(Event.COMPLETE, onLoaded);
loader.dataFormat=URLLoaderDataFormat.TEXT;
loader.load(myData);
function onLoaded(e:Event):void {
MovieClip(this.root).gotoAndStop("main");
}
Click Handler
public function SaveProfileButton() {
addEventListener(MouseEvent.CLICK, mouseClick);
}
private function mouseClick(e:MouseEvent):void{
MovieClip(this.root).output.text = "loading...";
send();
}
send() is the code to do the request.
Thanks
I think you may need a cross domain policy xml on your server where the php is. 99% sure. http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html

Flash receiving status of file write from PHP?

I am trying to make a simple Flash ActionScript3 program that saves some text to a text file (on my server) via a PHP script. I want the Flash program to be able to detect if the PHP script fails to write. Right now I'm just trying to get Flash to trace a status received from PHP. My below code is based on a few examples I've found online.
Here is my Flash code:
import flash.net.*;
import flash.events.*;
var varLoader:URLLoader = new URLLoader;
var varURL:URLRequest = new URLRequest("http://xxxxxxxx/outputTest.php");
var submittedData:URLVariables=new URLVariables();
varURL.data = submittedData;
varURL.method = URLRequestMethod.POST;
submittedData.inputData = "ThisIsTheDataToBeSaved";
submittedData.FileName = "ThisIsTheFileName";
varLoader.addEventListener(Event.COMPLETE, fxnDoneSaving);
varLoader.load(varURL);
function fxnDoneSaving(evt:Event):void{
trace("Done saving.");
trace("Write status: "+String(evt.target.data.WasWritingSuccessful));
}
Here is my PHP code:
<?php
$receivedFromFlashData = $_POST['inputData'];
$receivedFromFlashFileName = $_POST['FileName'];
$filename = $receivedFromFlashFileName . ".txt";
$myTextFileHandler = fopen($filename,"w");
if($myTextFileHandler)
{$writeInTxtFile = #fwrite($myTextFileHandler,"$receivedFromFlashData");}
fclose($myTextFileHandler);
if ($writeInTxtFile)
{echo "WasWritingSuccessful=success";}
else
{echo "WasWritingSuccessful=failure";}
?>
When Flash gets to the final trace statement, I get the following error:
"ReferenceError: Error #1069: Property WasWritingSuccessful not found on String and there is no default value."
Please help me understand what I'm doing wrong? thanks!
You need to specify the dataFormat of varLoader like so: varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

AS2: load class variables with sendandload

I'm using Actionscript 2.0 in combination with PHP, now I can make a call to my PHP file and receive data but apparently I have to use that data immediately, I cannot use it to fill my class variables.
This is what I want :
class user {
var lastname:String;
function user(in_ID:Number){
var ontvang:LoadVars = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
ontvang.onLoad = function(success:Boolean) {
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
};
}
}
I've found out that this is a big issue in AS2, I found this post to work around it if you're loading XML data but I can't seem to get it to work with LoadVars :
http://www.actionscript.org/forums/showthread.php3?t=144046
Any help would be appreciated ..
When your onLoad handler is called, it is being called as if it were a member function of the LoadVars instance, and not your user instance.
There are several ways around this, one is to use Delegate.create() to create a function which will work as intended, for example:
import mx.utils.Delegate;
class user {
var lastname:String;
var ontvang:LoadVars;
function user(in_ID:Number){
ontvang = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
ontvang.onLoad = Delegate.create(this, onLoad);
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
};
}
function onLoad(success:Boolean) : Void
{
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
}
}
Don't forget that the load is asynchronous - when you create one of your user objects, the member variables won't be immediately available. What you may need to do is let your user object be capable of signaling its readiness much like LoadVars does, (e.g. with a callback function provided by the caller) so that your app is driven by by these asynchronous events.

Categories