我是两种编程语言的新手,可以使用任何可能的帮助。我在AS3中创建了一个表单,数据应该发送到PHP然后发送到MYSQL。我检查了我的代码,并且在PHP文档或AS3文档中都没有错误。
我正在使用WAMP Server 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());
}
}
?>
答案 0 :(得分:1)
您忘了将phpVars添加到请求中。
myRequest.data = phpVars;
修改强>
在调用load方法之前,还应该将事件侦听器添加到loader中。
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, response);
loader.load(myRequest);
答案 1 :(得分:1)
您需要将URLRequest()
中PHP脚本的路径更改为本地服务器上的完整路径。我的意思是,如果您在本地运行服务器,则需要将路径更改为:
var myRequest:URLRequest = new URLRequest("http://localhost/form.php");
我准备了一个可以使用的工作示例,您可以在这里下载:
它包含以下内容: