操作脚本 3,注册,数据发送到 php,但 AS3 不会显示数据,但没有错误


Action script 3, Registration, Data sent to php, but AS3 won't display data back but have no Error

我是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" ;    

 }
 }

?>

非常感谢您的时间和帮助

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

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

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

这将为您提供从 PHP 返回的字符串。 您可以使用 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");
}