Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

actionscript 3 - Sending and receiving data from Flash AS3 to PHP

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:

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:

<?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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

<?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 :-)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...