PDA

View Full Version : $_POST to text file from Flash form


CWD
05-20-2006, 03:58 AM
:thumbsup: I'm trying to figure out how to handle extracting vars from $_POST array and then formatting them to be written to a text file that can in turn be read by the flash file that submitted the $_POST in the first place.

The vars will need to be generated from expressions because there will not always be the same number of vars. The vars will be in sets of 3 like this:

$expire_1
$recipient_1
$request_1
$expire_2
$recipient_2
$request_2
etc etc etc

There could be anywhere from 0-100 sets of these vars at any given time.

They will need to be formatted like this for writing to the text file so my flash file can read the file:

&expire_1=value&
&recipient_1=value&
&request_1=value&
&expire_2=value&
&recipient_2=value&
&request_2=value&
etc etc etc

I am thinking that something like this code may be heading in the right direction for defining the variables after they are extracted from the $_POST array:

for ($i=0, $j=1; $j <= var_entries; $i++, $j++) {
${"expire_{$j}"} = $_POST['expire_'.$i];
$i++;
${"recipient_{$j}"} = $_POST['recipient_'.$i];
$i++;
${"request_{$}j"} = $_POST['request_'.$i];
}
}

I need help :confused: with this code above as I doubt that I have valid code [trying to adapt Action Script to PHP through studying examples on this board, in the php manual and in some books I have].

I also need help with understanding how I would adapt the following code to write the vars to the text file using a "var_entries" variable in the $_POST array so I can generate the right number of var sets

if(is_writable('request.txt'))
{
$fp = fopen(request.txt','w');
$content = "&".$expire_1."&\n&".$recipient_1."&\n&".$request_1."&\n" //etc etc;

fwrite($fp,$content);
fclose($fp);
}
else
{
echo'File is not writable';
}

Where I'm stumped on this code is how to create the proper string for the $content var -- can I use another for loop for this somehow?

mic2100
05-20-2006, 08:08 AM
$i=0;

for ($j=1; $j = var_entries; $j++) {
${"expire_{$j}"} = $_POST['expire_'.$i];

${"recipient_{$j}"} = $_POST['recipient_'.$i];

${"request_{$}j"} = $_POST['request_'.$i];

$content .= "&".$expire_$j."&\n&".$recipient_$j."&\n&".$request_$j."&\n"; //etc etc;

$i++;
}
}

$filename = 'yourfile.txt';

if (!$handle = fopen($filename, 'w'))
{
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $content) === FALSE)
{
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($content) to file ($filename)";

fclose($handle);


This should help u a bit. :thumbsup:

mic2100
05-20-2006, 08:11 AM
but i'm not to sure about the 'var_entries' that you have placed in there.

CWD
05-20-2006, 07:47 PM
Thanks a bunch mic2100. :thumbsup:

The "var_entries" var will be in the $_POST array submitted by my flash form so I believe I can define it like this in the php:

$var_entries = $_POST['var_entries'];

That should work hey?

I'll give this a whirl and let you know how it goes. :D

CWD
05-21-2006, 07:19 PM
Hey mic2100:

I found a syntax errors in this part of your code:

${"request_{$}j"} = $_POST['request_'.$i];

should be:

${"request_{$j}"} = $_POST['request_'.$i];

I also am wondering about this code:

content .= "&".$expire_$j."&\n&".$recipient_$j."&\n&".$request_$j."&\n";

Shouldn't this have the dot concatenator between the vars like this?:

content .= "&".$expire_.$j."&\n&".$recipient_.$j."&\n&".$request_.$j."&\n";

Mahalo for the help! :thumbsup:

mic2100
05-21-2006, 08:49 PM
${"request_{$}j"} = $_POST['request_'.$i];

should be....

${"request_$j"} = $_POST['request_'.$i];

the .= adds to the variable

$content .= $value;
or
$content = $content + $value;

sorry it was a bit early in the morning.

mic2100
05-21-2006, 08:50 PM
for the 3rd part of ur question yes they should be joined like that

CWD
05-22-2006, 05:19 AM
Hey mic2100, I got it all working as I needed thanks to your help my friend! :thumbsup: :thumbsup: :thumbsup: