# AS3 Cannot communicate with php.

**URL:** <https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072>\
**Category:** flash\
**Created:** [April 13, 2018, 2:27pm UTC](https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072 "2018-04-13T14:27:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajoo](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@ajoo](https://forum.kirupa.com/u/ajoo)\
**Post date:** [April 13, 2018, 2:27pm UTC](https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072/1 "2018-04-13T14:27:00Z")

</div>

Hi,

I have spent the whole day getting this seemingly simple piece of code to work but it stubbornly refuses to.

```auto
var variables:URLVariables = new URLVariables();	

var varSend:URLRequest = new URLRequest("comm.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;

var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

	variables.user1 = "Guest";
	variables.user2 = "Newbie";
	variables.sendRequest = "parse";
	
	// send data to php
	varLoader.load(varSend);
	varLoader.addEventListener(Event.COMPLETE, completeHandler);

	function completeHandler(event:Event):void
	{
		trace(event.target.data.var1);
		var var1 = event.target.data.var1;
		var var2 = event.target.data.var2;
		var var3 = event.target.data.var3;
	}
        // Three textboxes on stage.
	str1.text = var1;
	str2.text = var2;
	str3.text = var3;
	

```

The php code is as follows

```auto
    ($_POST['sendRequest'] ==="parse"){`

	// var_dump($_POST);
	
	$user1 = $_POST['user1'];
	$user2 = $_POST['user2'];
	
	$var1 = "Box of";
	$var2 = "chocolates";
	$var3 = "is packed";
	
	echo "&var1=".$var1;
	echo "&var2=".$var2;
	echo "&var3=".$var3;
	
	exit();
    }

```

I tried numerous formatting of return strings like ( as in many different examples on google)

echo “var1=$var1”;,  
echo “var1 =”.$var1;.  
echo “&var1 =”.$var1;.  
.  
.

and so on,  
another, all in one string as below  
returnString = “&var1=”.$var1."&var2=".$var2."&"; and whatever I could think of.  
(this worked in AS2 sendAndLoad very well but here I am grappling with AS3.)

I keep getting the following error.

```auto
TypeError: Error #2007: Parameter text must be non-null.
	at flash.text::TextField/set text()
	at AS3_swf_php_comm_fla::MainTimeline/AS3_swf_php_comm_fla::frame1()

```

Please help !!

Thanks all !

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [April 13, 2018, 2:45pm UTC](https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072/2 "2018-04-13T14:45:37Z")

</div>

You have to set your `.text` properties in `completeHandler`. Your var variables only exist there, and aren’t defined until after the php variables load. You know when it loads within `completeHandler`.

---

<div class="post-metadata">

**Author:** ![ajoo](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@ajoo](https://forum.kirupa.com/u/ajoo)\
**Post date:** [April 13, 2018, 5:04pm UTC](https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072/3 "2018-04-13T17:04:23Z")

</div>

Thanks Senocular,

You are right but I had tried by defining the variables var1, var2 and var3 at the very beginning of the program ( lines 1,2 and 3) and then shifting the .text variables outside. Nogo.

I have also tried by keeping them within the completeHandler function but the result was the same.

However I will try it all once again and revert.

Thank you.

---

<div class="post-metadata">

**Author:** ![ajoo](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@ajoo](https://forum.kirupa.com/u/ajoo)\
**Post date:** [April 13, 2018, 5:11pm UTC](https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072/4 "2018-04-13T17:11:57Z")

</div>

Hi Senocular !!

I’ld be ■■■■■■ !! It worked ! I swear I tried this tens of time during the day and it did not work. I guess you have the midas touch !!

Thanks a ton.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [April 13, 2018, 5:33pm UTC](https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072/5 "2018-04-13T17:33:17Z")

</div>

Its not just scope, its also _timing_. Putting the vars at the top of the page would fix the scope (access to use the variables) but not the timing (the point in time in which they’re defined).

The reasons callbacks are used in things like `URLLoader.load()` is because the loading of a remote document takes time, and other code shouldn’t have to wait for that load to happen to keep running. So instead of halting all code that _could_ run during a load, that code is allowed to run and the code that needs to react to the completion of the load is contained within a callback that gets executed when the document loaded.

Anything in the same frame outside of the callback gets run before the callback, even if its written after the callback.

```auto
// RUNS FIRST ...

var variables:URLVariables = new URLVariables();	
var varSend:URLRequest = new URLRequest("comm.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
variables.user1 = "Guest";
variables.user2 = "Newbie";
variables.sendRequest = "parse";
varLoader.load(varSend);
varLoader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void
{

    // RUNS THIRD, after all other code already ran ...

    trace(event.target.data.var1);
    var var1 = event.target.data.var1;
    var var2 = event.target.data.var2;
    var var3 = event.target.data.var3;
}

// RUNS SECOND (or as part of FIRST) ...
// vars have _not_ been set to any values yet

str1.text = var1;
str2.text = var2;
str3.text = var3;

```

---

<div class="post-metadata">

**Author:** ![ajoo](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@ajoo](https://forum.kirupa.com/u/ajoo)\
**Post date:** [April 13, 2018, 5:37pm UTC](https://forum.kirupa.com/t/as3-cannot-communicate-with-php/638072/6 "2018-04-13T17:37:45Z")

</div>

Hi Senocular,

> Its not just scope, its also timing. Putting the vars at the top of the page would fix the scope (access to use the variables) but not the timing (the point in time in which they’re defined).

Yes that occurred to me too, after I had just posted the last reply and was going through it. But thanks for the further clarification all the same. Much obliged !!🙂

Thank you
