0

I have build a website and communicate with actionscript. in actionscript i'm build a function to call php function and post a variables for load data from mysql database.The problem is when i call actionscript function and post variables to php.The php side i call $_POST['action']; for receive a variables from actionscript side but when i want to see this Post $_POST['action']; it error like this

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

and this is a action script function to call 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;
    }

and this is a php function side 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);
}



?>

What wrong? Pls any idea for this and Thank.

King_Dark
  • 299
  • 1
  • 4
  • 14
  • at a cursory glance I would presume that `variables.action = SELECT;` did not execute, which probably means `SELECT` was undefined. Could you include where you declare and initialise that variable? – CyanAngel Oct 10 '14 at 14:21
  • Oh Yes i remember comment that.I'm already initialise 'SELECT' is String at up side of file. – King_Dark Oct 13 '14 at 03:14

1 Answers1

0

I do not know if you've found a solution for your problem, but this may help someone else.

1 - Take a look here, It can help you : Submitting scores from AS3 to PHP/SQL - #Error 2101

2 - To avoid PHP Undefined index Notice, you should always verify if your var is set before use it, like what you've done at line 7.

3 - Avoid all outputs that are not used by your AS script in the PHP script because AS will just get the first output.

4 - If you use a for or foreach loop to get data in your PHP script, you should do like this because echo that will send data to AS should be executed once :

$result = '';
$sep = ',';
foreach($row as $cname => $cvalue){
    $result .= $cvalue . $sep;
}
echo 'Result='.$result;
Community
  • 1
  • 1
akmozo
  • 9,829
  • 3
  • 28
  • 44