0

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());
        }
}
?>  
3
  • Hi DP182, it looks like this is the 3rd / 4th time you've asked this question. Have you tried something a little simpler to ensure all the mechanics are working as expected? For example, just make a basic Flash application that will send one piece of data to PHP. Then make the PHP script send that back to Flash with some manipulation (like uppercasing) and see if you're getting the result you expect. From there you will be able to start laying on the more complicated parts like your SQL queries and multiple data fields. Commented Mar 11, 2013 at 23:22
  • I am preparing a bundle for you now with some basics that you can run and make sure everything is working OK. From there you'll be able to build on what I supply and hopefully get to where you want to be :) Commented Mar 11, 2013 at 23:29
  • It would be helpful if you properly indented your code. Commented Mar 12, 2013 at 13:13

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

9 Comments

I added that line but the data still wont send... once i click submit form the data should send and the data in the fields should be erased... none of this happens any ideas why ?
The fields are never erased because you never call completeHandler().
Just to be sure, you are assigning phpVars to myRequest.data before you call load() on the loader?
Yes I put myRequest.data before the load()... and how would i call completeHandler() ? sorry to sound stupid, but I'm new to these programming languages.
You should add completeHandler as a listener on loader, just like you did with response(). I found another potential problem with your code. Please see my edit above, and let me know if that makes any difference. If you are still having problems, try wrapping your call to load in a try catch block like in the 2nd example on this page and see if you get any errors.
|
1

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.

2 Comments

when i checked the php i get an Undefined index: testString error. I've looked at the way you've connected it up... but dont understand it... could you explain it if possible?
I've changed my actionscript code, does that make it better or worse and does it seem correct? i renamed the PHP var to just variables and restructured it? I appreciate your help thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.