The basics of getting Flash to talk to PHP is to upgrade your actionscript version. Don't use anything lower than 2. AS 2 makes it easier to talk with external scripts. Using AS 2 you can use the function sendAndLoadVars() to send data and get a response back.
Using AS 3 you have to use a couple classes, namely URLLoader and URLVariables, to send data to an external script.
The basics of it are this (I'm going to use AS 2 since I just got done backporting an AS 3 form to AS 2):
-
Create the form elements
-
Assign names to the elements (using the property inspector)
-
Assign actionscript to the submit button to call a submit() function we'll define
-
Assign actionscript to the reset button to call a reset() function we'll define
The basics of the actionscript are this:
function reset():Void {
full_name.text = '';
email.text = '';
phone.text = '';
message.text = '';
}
function submit():Void {
// Do some basic validation
if(full_name.text == '' || email.text == '' || phone.text == '' || message.text == '') {
// Display an error message
}
else
{
var result:LoadVars = new LoadVars();
result.onLoad = function(success:Boolean) {
if(success) {
// Display success message
}
else {
// Display error message
}
};
var send:LoadVars = new LoadVars();
// Assign data to the POST
send.email = email.text;
send.fullName = full_name.text;
send.phone = phone.text;
send.message = message.text;
// Send the request via POST to a url
send.sendAndLoad('http://www.domain.tld/myscript.php', result, 'POST');
}
}
Now, with those once you send the data your php script can access the data in the POST array like:
<?php
$_POST['email']; // The user supplied email address
$_POST['fullName']; // The user supplied full name
$_POST['phone']; // The user supplied phone number
$_POST['message']; // The user supplied message
Now, if you want to allow "talking" between PHP and Flash, you need to structure your response from PHP to flash just like the query string of a $_GET request in the url. So to send data back, you'd structure it like so:
&someVar=some string&someOtherVar=some more data you want sent back&anotherVar=some more information!
Then in flash, you can reference the response object (in our code above it's "result"). To do that, you would do it like so:
var result:LoadVars = new LoadVars();
result.onLoad = function(success:Boolean) {
if(success) {
// Display success message
trace(result.someVar);
trace(result.someOtherVar);
trace(result.anotherVar);
}
else {
// Display error message
}
};
Hope that helps you start somewhere....