我对这两种编程语言都不熟悉,可能需要任何帮助。我在AS3中创建了一个表单,数据应该发送到PHP,然后再发送到MYSQL。我已经检查了我的代码,在PHP文档或AS3文档中都没有错误。
我正在使用WAMP服务器2.2,我确信我的文档是正确的,即WAMP/WWW/Project
(文档在此文件夹中)。我可以通过http://localhost
访问它们,但是没有发送数据。
这是我的PHP和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());
}
}
?>
您忘记在请求中添加php参数了
myRequest.data = phpVars;
编辑
您还应该在调用load方法之前将事件侦听器添加到loader。
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, response);
loader.load(myRequest);
您需要将URLRequest()
中PHP脚本的路径更改为本地服务器上的完整路径。我的意思是,如果您在本地运行服务器,则需要将路径更改为如下内容:
var myRequest:URLRequest = new URLRequest("http://localhost/form.php");
我已经准备了一个可以使用的例子,你可以在这里下载:
- PHP到AS3通信的例子。
包含以下内容:
- 应用程序。你可以在这里的第一帧看到工作代码。
- 请求。as -一个简单的类,我为你更容易地管理这个过程。
- DemoRequestResponse.php -我为演示制作的一个简单的PHP脚本。