DP187
DP187

Reputation: 11

Sending data from AS3 to MySQL via PHP

I'm new to both programming languages and could use any help possible. I've created a form in AS3 and the data should be sent to PHP and then to MYSQL. I've checked my code and have no errors in either the PHP document or the AS3 document.

I'm using WAMP Server 2.2 and I'm sure my documents are in the correct which is WAMP/WWW/Project (documents are in this folder). I can access them via http://localhost but the data is not being sent.

Here is my coding for both PHP and AS3

AS3:

// hide processing CM
processing_mc.visible = false;

var variables:URLVariables = new URLVariables();

// Build the varSend variable
var varSend:URLRequest = new URLRequest("http://localhost/belitefitness/form.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;

// Build the varLoader variable
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
// handler for the PHP script completion and return of status
function completeHandler(event:Event):void {

// remove processing clip
processing_mc.visible = false;
firstname_txt.text = "";
lastname_txt.text = "";
email_txt.text = "";
number_txt.text = "";
msg_txt.text = "";

// Load the response from php here
status_txt.text = event.target.data.return_msg;
}
// Add event listener for submit button click
submit_btn.addEventListener(MouseEvent.CLICK, ValidateAndSend);

// function ValidateAndSend
function ValidateAndSend (event:MouseEvent):void {

// validate fields
if(!firstname_txt.length) {
status_txt.text = "Please enter your First Name";
} else if (!lastname_txt.length) {
status_txt.text = "Please enter your Last Name";
} else if (!email_txt.length) {
status_txt.text = "Please enter your Email";
} else if (!number_txt.length) {
status_txt.text = "Please enter your Phone Number";
} else {

// All is good, send the data now to PHP
processing_mc.visible = true;

// ready the variables in our form for sending
variables.userFirstName = firstname_txt.text;
variables.userLastName = lastname_txt.text;
variables.userEmail = email_txt.text; 
variables.userNumber = number_txt.text;
variables.userMsg = msg_txt.text; 
// Send the data to PHP now
varLoader.load(varSend);
} // close else condition for error handling
} // close validate and send function

PHP

<?php 
if(isset($_POST['userFirstName']) && isset($_POST['userLastName']) && isset($_POST['userEmail']) && isset($_POST['userNumber']) && isset($_POST['userMsg']))
{
    $userFirstName=strip_tags($_POST['userFirstName']);
    $userLastName=strip_tags($_POST['userLastName']);
    $userEmail=strip_tags($_POST['userEmail']);
    $userNumber=strip_tags($_POST['userNumber']);
    $userMsg=strip_tags($_POST['userMsg']);

    // connect with database. 

    $username="root";
    $password="dp10aap";
    $database="b-elite-fitness";

    mysql_connect("localhost","$username","$password") or die (mysql_error());
    mysql_select_db("$database") or die (mysql_error());

    //query for inserting data in database.
    $query="INSERT INTO 'formdp' VALUES('NULL','".mysql_real_escape_string($userFirstName)."','".mysql_real_escape_string($userLastName)."','".mysql_real_escape_string($userEmail)."','".mysql_real_escape_string($userNumber)."','".mysql_real_escape_string($userMsg)."')";

    if($query_run=mysql_query($query))
    {
        echo'Data inserted.';
    } else {
        die(mysql_error());
        }
}
?>  

Upvotes: 0

Views: 3813

Answers (2)

Marty
Marty

Reputation: 39456

You need to change the path of your PHP script in your URLRequest() to the full path on your local server. By this I mean, if you're running a server locally, you'll need to change your path to something like:

var myRequest:URLRequest = new URLRequest("http://localhost/form.php");

I have prepared a working example for you to use that you can download here:

It contains the following:

  • Application.fla - you can look on the first frame here to see the working code.
  • Request.as - a simple class I made for you to more easily manage this process.
  • DemoRequestResponse.php - a simple PHP script I made for the demo as well.

Upvotes: 1

bwroga
bwroga

Reputation: 5459

You forgot to add your phpVars to the request.

myRequest.data = phpVars;

Edit
You should also add your event listeners to loader before you call the load method.

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, response);
loader.load(myRequest);

Upvotes: 1

Related Questions