Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-06-2008, 11:42 PM   #1
golden14
Registered User
 
Join Date: Jun 2007
Posts: 37
Default PHP POSTing many variables - loop?

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:

ActionScript 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 Code:
<?php

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

$numVars 0;

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

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

    
$numVars ++;
}

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

Any ideas how I can achieve this? Thanks in advance for the help!
golden14 is offline   Reply With Quote
Old 03-07-2008, 02:45 PM   #2
yell0wdart
jordanrift.com
 
Join Date: Sep 2007
Location: Phoenix, AZ
Posts: 366
Default

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.
__________________

bad developer

Jordan Rift
yell0wdart is offline   Reply With Quote
Old 03-07-2008, 05:58 PM   #3
golden14
Registered User
 
Join Date: Jun 2007
Posts: 37
Default

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 Code:
<?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 Code:
<?php

$numVariables 
0;

function 
writeVariable$name$value )
{
  if( 
$numVariables )
  {
    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!
golden14 is offline   Reply With Quote
Old 03-07-2008, 09:14 PM   #4
yell0wdart
jordanrift.com
 
Join Date: Sep 2007
Location: Phoenix, AZ
Posts: 366
Default

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.
__________________

bad developer

Jordan Rift
yell0wdart is offline   Reply With Quote
Old 03-07-2008, 10:05 PM   #5
golden14
Registered User
 
Join Date: Jun 2007
Posts: 37
Default

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 is offline   Reply With Quote
Old 03-08-2008, 06:46 PM   #6
golden14
Registered User
 
Join Date: Jun 2007
Posts: 37
Default

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 Code:
<?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 Code:
<?php

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

$tName[$i]=$_POST['tName[$i]'];
echo 
"tName$i"=.$tName$i;
}
?>
Any help with that syntax? Thanks!

Last edited by golden14; 03-08-2008 at 07:19 PM.
golden14 is offline   Reply With Quote
Old 03-08-2008, 07:29 PM   #7
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

You use { } to create a dynamic variable in PHP... So, your code will look something like this...
PHP Code:
<?php
    
for ($i $i $i++)
    {
        ${
"tName" $i} = $_POST['tName' $i];
        echo(
"&tName$i=" . ${"tName" $i});
    }
?>
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 03-08-2008, 08:33 PM   #8
golden14
Registered User
 
Join Date: Jun 2007
Posts: 37
Default

Great, thanks for the help!
golden14 is offline   Reply With Quote
Old 03-09-2008, 12:30 AM   #9
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Glad to help...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make FLASH import PHP variables... unbelievable Server-Side Scripting 1 12-12-2006 07:50 AM
Loading variables to an external PHP file? dr.wunder ActionScript 1.0 (and below) 0 10-22-2006 11:30 AM
Dynamically Creating Variables inside a loop davydany ActionScript 2.0 11 09-06-2006 06:14 AM
posting variables to my sql via php carlwelch ActionScript 2.0 1 12-14-2004 08:55 AM
passing variables to flash from php Dozey ActionScript 1.0 (and below) 8 01-16-2003 10:52 PM


All times are GMT. The time now is 02:39 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.