从 Flash AS3 向 PHP 发送和接收数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6876588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:31:00  来源:igfitidea点击:

Sending and receiving data from Flash AS3 to PHP

phpactionscript-3

提问by Julio Mendoza

I know this is frequently asked, but I have looked all over the internet to find the mistake I'm making with the code I've used to send and receive data from AS3 to PHP and viceversa. Can you find the mistake? Here is my code:

我知道这是经常被问到的问题,但我已经在互联网上查找了我在用于从 AS3 向 PHP 发送和接收数据的代码中犯的错误,反之亦然。你能找出错误吗?这是我的代码:

AS3:

AS3:

import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.events.Event;

submitbtn.addEventListener(MouseEvent.CLICK, sendData)

function sendData(event:MouseEvent):void
{
    var loader : URLLoader = new URLLoader;
    var urlreq:URLRequest = new URLRequest("http://[mydomain]/test.php");
    var urlvars: URLVariables = new URLVariables;
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    urlreq.method = URLRequestMethod.POST;
    urlvars.uname = nametxt.text;
    urlvars.apellido = aptxt.text;
    urlvars.email = emtxt.text;
    urlvars.cedula = cctxt.text;
    urlvars.score = scoretxt.text;
    urlreq.data = urlvars;
    loader.addEventListener(Event.COMPLETE, completed);
    loader.load(urlreq);
}

function completed(event:Event): void
{
    var loader2: URLLoader = URLLoader(event.target);
    trace(loader2.data.done);
    resptxt.text = event.target.data.done;
}

PHP inside of [domain]/test.php:

[域]/test.php 中的 PHP:

<?php
    $username = $_POST["uname"];
    $apellido = $_POST["apellido"];
    $cedula = $_POST["cedula"];
    $email = $_POST["email"];
    $score = $_POST["score"];
    print_r($_POST);
    if (!($link=mysql_connect(databasemanager,username,password))) 
       { 
          echo "Error conectando a la base de datos."; 
          exit(); 
       } 
       if (!mysql_select_db(database,$link)) 
       { 
          echo "Error seleccionando la base de datos."; 
          exit(); 
       }
       try
       {
           mysql_query("insert into scores(name,lastName,email,document,score) values('$username','$apellido','$email','$cedula','$score')",$link);                
           print "done=true";          
       }
       catch(Exception $e)
       {
           print "done=$e->getMessage()";          
       }
       echo "done=true";    
?>

Thanks for your answers.

感谢您的回答。

采纳答案by Kaken Bok

Your AS code seems to be right. So the problem might be in PHP. Please test first with this PHP file:

您的 AS 代码似乎是正确的。所以问题可能出在PHP中。请先用这个 PHP 文件进行测试:

<?php
       echo "test=1&done=true";    
?>

This should then let your movie trace "true". You then should debug your PHP. print_r($_POST);destroys your output of course. May be you did forget to remove this debugging statement :-)

这应该让你的电影追踪"true"。然后你应该调试你的 PHP。print_r($_POST);当然会破坏你的输出。可能您确实忘记删除此调试语句:-)

@Jesse and @Ascension Systems, check the docs for URLVariables: http://livedocs.adobe.com/flash/9.0_de/ActionScriptLangRefV3/flash/net/URLVariables.html

@Jesse 和 @Ascension Systems,检查 URLVariables 的文档:http://livedocs.adobe.com/flash/9.0_de/ActionScriptLangRefV3/flash/net/URLVariables.html

回答by Jesse

Try

尝试

submitbtn.addEventListener(MouseEvent.CLICK, sendData);

function sendData(event:MouseEvent):void
{
  var urlreq:URLRequest = new URLRequest ("http://[mydomain]/test.php");
  urlreq.method = URLRequestMethod.POST; 

  var urlvars:URLVariables = new URLVariables(); 
  urlvars.uname = nametxt.text;
  urlvars.apellido = aptxt.text;
  urlvars.email = emtxt.text;
  urlvars.cedula = cctxt.text;
  urlvars.score = scoretxt.text;
  urlreq.data = urlvars;          

  var loader:URLLoader = new URLLoader (urlreq); 
  loader.addEventListener(Event.COMPLETE, completed); 
  loader.dataFormat = URLLoaderDataFormat.VARIABLES; 
  loader.load(urlreq); 
}

public function completed (event:Event):void{
  var variables:URLVariables = new URLVariables( event.target.data );
  resptxt.text = variables.done;
}

Updated the completed function... and corrected missing bracket.

更新了完成的功能...并更正了缺失的括号。

回答by Kaken Bok

First of all, change this line of code:

首先,更改这行代码:

trace(loader2.data.done);

to this:

对此:

trace(loader2.data);

You're outputting raw text from php, so your data object in flash is just gonna be raw text. It's not an object with .done attached to it. If you want to have a data structure then you need to create some XML or something inside PHP, print that out and then cast loader2.data as XML, like so:

你从 php 输出原始文本,所以你在 flash 中的数据对象只是原始文本。它不是一个附加了 .done 的对象。如果你想要一个数据结构,那么你需要在 PHP 中创建一些 XML 或其他东西,打印出来然后将 loader2.data 转换为 XML,如下所示:

var returnedData:XML = new XML(loader2.data);

However, if your XML is not formed correctly, you'll create an uncaught error in flash and crash your app, so make sure you use try/catch statements.

但是,如果您的 XML 格式不正确,您将在 flash 中创建一个未捕获的错误并使您的应用程序崩溃,因此请确保使用 try/catch 语句。