PHP无法从ActionScript3接收参数

时间:2014-10-10 08:46:09

标签: php mysql actionscript-3 flash actionscript

我已经建立了一个网站并与actionscript进行交流。在actionscript中我构建了一个函数来调用php函数并从mysql数据库发布加载数据的变量。问题是当我调用actionscript函数并将变量发布到php时.php方面我称之为$ _POST [&#39 ;动作'];从actionscript方面接收变量,但是当我想看到这个Post $ _POST [' action'];像这样的错误

Notice: Undefined index: action in C:\wamp\www\MPA-EM\bin\model.php on line 3

这是一个调用php的动作脚本函数:

   public function SelectData(TBN:String,TYPE:String):void{
        var myrequest:URLRequest = new URLRequest("http://localhost/MPA-EM/bin/model.php");
        myrequest.method = URLRequestMethod.POST;

        var variables:URLVariables = new URLVariables();
        variables.tablename = TBN;
        variables.action = TYPE;
        myrequest.data = variables;

        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        loader.addEventListener(Event.COMPLETE, dataOnLoad);
        loader.addEventListener(Event.CANCEL, dataError);

        try{
            loader.load(myrequest);
        }catch(e:Error){
            Alert.show(e.toString());
        }

    }

    public function dataOnLoad(evt:Event):void
    {
        Alert.show(evt.target.data.Result);
        if(evt.target.data.Result) {
            DText.text = 'ok';
        } else DText.text = "Error in select submitted data";

        //status is a custom flag passed from back-end
    }
    public function dataError(e:Event) :void{

        DText.text = e.target.errormsg;
    }

这是一个php函数方面的model.php:

<?php 
//test for recive
    $actionW = $_POST['action'];//error this line.
    echo $actionW;
// end test

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    echo $action;
    switch($action) {
        case 'select' : 
                    $tablename = clean($_POST['tablename']);
                    selectedData($tablename);
                    break;

        case 'blah' : blah();break;
    }
}

function selectedData($table){
    // create connection
    $connection = mysql_connect("localhost", "root", "") or die ("Couldn't connect to the server.");

    // select database
    $db = mysql_select_db("ideaddcom_maps", $connection) or die ("Couldn't select database.");
    // create SQL
    $sql = 'SELECT * FROM '.$table;

    // execute SQL query and get result
    echo $sql;
    $sql_result = @mysql_query($sql, $connection) or die ("Couldn't execute query.".mysql_error());


    $row = mysql_fetch_object($sql_result);


    foreach($row as $cname => $cvalue){
        echo "Result=$cvalue";

     }

    // free resources and close connection
    mysql_free_result($sql_result);
    mysql_close($connection);

}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
    $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}



?>

有什么不对?请提出任何想法,谢谢。

1 个答案:

答案 0 :(得分:0)

我不知道您是否为您的问题找到了解决方案,但这可能有助于其他人。

1 - 看看这里,它可以帮助你:Submitting scores from AS3 to PHP/SQL - #Error 2101

2 - 为了避免 PHP Undefined index 注意,您应该在使用之前验证您的var是否已设置,就像您已经完成的那样在第7行。

3 - 避免 PHP脚本 AS脚本未使用的所有输出,因为 AS 将获得第一个输出。

4 - 如果您使用forforeach循环来获取 PHP脚本中的数据,则应该这样做,因为echo会发送数据到 AS 应该执行一次:

$result = '';
$sep = ',';
foreach($row as $cname => $cvalue){
    $result .= $cvalue . $sep;
}
echo 'Result='.$result;