Archive for January, 2008

Sending form data with Actionscript 3

So I just spent a few hours trying to figure out how to submit form data with Actionscript. It really isn't very hard, so I have discovered after a little research. I am going to show you an example of how to do it which I wouldn't describe as "best practice" but it works.

The reason I describe this as less than best practice is because when you send data to the sever you get no response. So, even though it works 99.9 per cent of the time, you do not get a confirmation that the server received your request.

All my client wants is for a user to be able to enter his / her email address in to a box and have and email sent and then add that email address to an email. So really we are just sending some text to my client from a form. We want the user to enter the text and click send and have it be sent without another browser window opening.

The model for this is as follows: flash will send data to a URL with the POST method and a PHP file will get the data and send an email to my client.

So we need 2 things:

The actionscript...

Actionscript:
  1. var request:URLRequest = new URLRequest( "http://mysite.com/submit.php" );
  2.  
  3. var variables:URLVariables = new URLVariables();
  4. variables.email = emailSubmitText;
  5.  
  6. request.data = variables;
  7.  
  8. request.method = URLRequestMethod.POST;
  9.  
  10. sendToURL(request);

and the PHP:

PHP:
  1. $emailAddress = $_POST['email'];
  2.  
  3. $to = "eddie@teamcolab.com";
  4. $subject = "You have a new subscriber";
  5. $body = $emailAddress;
  6. if (mail($to, $subject, $body)) {
  7. echo("Your message was sent");
  8. } else {
  9. echo("There was a problem.");
  10. }

Comments (11)