PDA

View Full Version : PHP POSTing many variables - loop?


golden14
03-07-2008, 12:42 AM
I'm trying to pass many variables from AS3 to PHP and then back to AS3. I can get this to work with one variable no problem. But I'm going to need to do this with about 50 variables, and figure I'll need to use a loop. I'm pretty sure the AS3 code is correct, it's the POST in PHP that I'm having trouble with. Here's the AS3 code:

var request:URLRequest = new URLRequest ("http://localhost/test.php");
request.method = URLRequestMethod.POST;

function getTextVariables( total:uint=50 ):URLVariables
{

var variables:URLVariables = new URLVariables();
var i:int = total;

while( i -- )
{
var name:String = "tName" + i;
variables[ name ] = this[ name ].text;
}

return variables;
}

request.data = getTextVariables( this );

var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);

function onComplete (event:Event):void
{
var variables:URLVariables = new URLVariables( event.target.data );
for (i=0; i<50; i++)
{
var name:String = "tField" + i;
this[ name ].text = variables.tName[i];
}
}

And here's the PHP code:

<?php

for (i=0; i<50; i++)
{
$tName[i] = $_POST['tName[i]'];
}

$numVars = 0;

function writeVar( $name, $value )
{
if( $numVars > 0 )
{
echo "&";
}

echo $name . "=" . urlencode($value);

$numVars ++;
}

for (i=0; i<50; i++)
{
writeVar( $tName[i], "tName[i]" );
}
?>


Any ideas how I can achieve this? Thanks in advance for the help!

yell0wdart
03-07-2008, 03:45 PM
First off, why are you assigning a value inside the arguments () of your first method? Generally, you pass values in there, not assign them. So when you make that method call, you'd pass 50 in, and then assign that value to a local variable inside your method.. ie: var i:int = total;

Secondly, does the decrementor operator implicitly return a boolean in AS3? Your while loop looks a little off to me. I've been doing more C# lately than AS, but I think that may be causing some of your problems... Basically, inside a while loop's parentheses you want to put a boolean (an object that is either set to true or false, an expression that will evaluate to true or false (while (i < total)) or a method call that will return true or false).

You may be better off using a for loop here if you know exactly how many times you need to iterate through it.

golden14
03-07-2008, 06:58 PM
Thanks, I took a look at that section of code, and it does make much more sense to use a for loop there. I changed it and that part works fine. I'm now more certain that its the PHP code that is incorrect.

When testing it out, it will successfully send the variables to PHP and return them to Flash - the problem is that it doesn't seem to break up the string correctly upon the return and gives an error.

Here is the PHP code that sort of works..

<?php

$tName1=$_POST['tName1'];
$tName2=$_POST['tName2'];

echo "tName1=".$tName1;
echo "tName2=".$tName2;
?>

What happens when it sends it back is that in the first text field, it shows something like "blah1tName2=blah2" and then doesn't show anything in the second text field and gives an error. So for whatever reason it's not splitting up the name/value pairs.

I also tried this code, but it doesn't work at all. I think it might be some sort of syntax thing that I'm not familiar with yet.

<?php

$numVariables = 0;

function writeVariable( $name, $value )
{
if( $numVariables > 0 )
{
echo "&";
}

echo $name . "=" . urlencode($value);

$numVariables++;
}

$tName1=$_POST['tName1'];
$tName2=$_POST['tName2'];

writeVariable( "tName1", $tName1 );
writeVariable( "tName2", $tName2 );

?>

And then there's the whole matter of using a loop in PHP since there's so many variables being passed, but I guess we'll just take this one step at a time. Thanks for the help!

yell0wdart
03-07-2008, 10:14 PM
Well, how are you passing it in? Are you passing data from AS to PHP more than once? If so, then it might work OK, but it'd seem if you're just passing your information in once, then you'd probably need to loop through it in PHP or set up a recursive method call. In your script, you're only calling your writeVariable() method twice. So if you pass more than two objects into your script, that may cause problems... especially if your AS is expecting more than 2 objects back.

golden14
03-07-2008, 11:05 PM
Yeah, there's going to be a lot of variables, probably around 50. I only have 2 in the example because I just wanted to try with more than 1.

I agree about using a loop in the PHP, I'm just not quite sure on the syntax to get it to work correctly. I think I'll need a loop for all the POSTings as well as a loop to use the writeVariable function. So far, the syntax that I've tried doesn't seem to work.

Thanks!

golden14
03-08-2008, 07:46 PM
I'm just trying to figure out what the correct syntax is here, because everything I'm trying hasn't worked so far.

Here's it not in a for loop, and this works:

<?php

$tName1=$_POST['tName1'];
$tName2=$_POST['tName2'];
$tName3=$_POST['tName3'];

echo "tName1=".$tName1;
echo "tName2=".$tName2;
echo "tName3=".$tName3;
?>

And here's my unsuccessful attempt at a loop:

<?php

for ($i=1; $i<4; $i++) {

$tName[$i]=$_POST['tName[$i]'];
echo "tName$i"=.$tName$i;
}
?>

Any help with that syntax? Thanks!

CyanBlue
03-08-2008, 08:29 PM
You use { } to create a dynamic variable in PHP... So, your code will look something like this...
<?php
for ($i = 1 ; $i < 4 ; $i++)
{
${"tName" . $i} = $_POST['tName' . $i];
echo("&tName$i=" . ${"tName" . $i});
}
?>

golden14
03-08-2008, 09:33 PM
Great, thanks for the help!

CyanBlue
03-09-2008, 01:30 AM
Glad to help... :)