0

我是 Php 和 AS3 的新手,我正在尝试设置一个注册页面,AS3 会将数据发送到 php,然后它应该返回一条消息说您已成功注册。目前,AS3 确实将所有数据发送到数据库。但是AS3没有显示注册成功的消息,但也显示没有错误。请帮忙

我的 AS3 代码

stop();



// build variable name for the URL Variables loader
var variables:URLVariables = new URLVariables;

// Build the varSend variable
var varSend:URLRequest = new URLRequest("http://localhost/dummy.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 {
  if(event.target.data && event.target.data.return_msg)
   status_txt.text = event.target.data.return_msg

} 


// Add event listener for submit button click
submit.addEventListener(MouseEvent.CLICK, ValidateAndSend);

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

// validate fields
if(!name_txt.length) {
status_txt.text = "Please enter your name";
} else if (!email_txt.length) {
status_txt.text = "Please enter your email";
} else if (!pass_txt.length) {
status_txt.text = "Please enter your password";
} else { 


// ready the variables in our form for sending
variables.username = name_txt.text;
variables.email = email_txt.text; 
variables.password = pass_txt.text;

        // if error occurs


// Send the data to PHP now
varLoader.load(varSend);


} // close else condition for error handling

}

下面是我的php代码

<?php

ini_set('display_errors', 1); error_reporting(E_ALL);
include 'connect.php';


{
 //get form data
 $username = ($_POST['username']);
 $password = ($_POST['password']);
 $email = ($_POST['email']);

 if (!$username||!$password||!$email)
 {
     $fill= "Please fill out all fields";
 echo ($fill) ;
 }

 else
 {
    //encrypt password
    $password = md5($password);

    //check if username already taken
    $check = mysqli_query($con,"SELECT * FROM Test WHERE username = '$username'") or die( mysqli_error());
    if (mysqli_num_rows($check)>=1)
    {
    echo "return_msg=Username_already_taken";}

    else
    {




           //register into database
            mysqli_query($con,"INSERT INTO Test (username,password,email) VALUES 
                ('$username','$password','$email');") or die(mysqli_error());
        }


    echo "return_msg=success" ;    


 }

 }


?>

非常感谢您的时间和帮助

4

1 回答 1

1

根据您在跟踪时的响应event.target.data,php 正在发送响应。由于条件逻辑,您什么也看不到。event.target.data 确实存在但不存在event.target.data.return_msg

您可以删除条件逻辑并拥有

function completeHandler(event:Event):void {
    status_txt.text = event.target.data
} 

这将为您提供从您的 php.ini 返回的字符串。您可以使用 trim 清除空格(测试响应中的 url_encoded 字符),然后根据需要将其拆分在=符号上。我个人喜欢使用JSON符号与 AS3 和 PHP 进行通信...

编辑添加:

Adobe Docs中提到了一个 StringHelper 类,但我在 DocumentClass 中使用以下函数来修剪响应:

public static function trim(s:String):String{
    return s.replace(/^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2");
}
于 2013-10-09T18:41:54.043 回答