0

I'm trying to use the if statement in flash to check if a variable (fetched from PHP) is equal to something, but something is going wrong.

The code:

function completeHandler(event:Event):void{
    // Load the response from the PHP file
    var data:URLVariables = new URLVariables(event.target.data);
    var return_stat = data.return_stat_verify;



    if (return_stat == "FAILED"){
    status_txt.text = "dsfdsfg";
        }
        else if (return_stat == "PASSED"){

        var first_nme = data.return_first;
        var second_nme = data.return_second;
    var email_addr = data.return_email;
    var user_domain = data.return_domain;
    var user_name = data.return_username;

    gotoAndPlay("finish");

    first_txt.text = first_nme;
    second_txt.text = second_nme;
    email_txt.text =  email_addr;
    username_txt.text = user_name;
    domain_txt.text = user_domain;


    }

Now when i test that, nothing happens. I then make an attempt to place a simple else statement in there to see if both if and else if statements fail.

the code:

function completeHandler(event:Event):void{
    // Load the response from the PHP file
    var data:URLVariables = new URLVariables(event.target.data);
    var return_stat = data.return_stat_verify;

    if (return_stat == "FAILED"){
    status_txt.text = "dsfdsfg";
    }
    else if (return_stat == "PASSED"){

    var first_nme = data.return_first;
    var second_nme = data.return_second;
    var email_addr = data.return_email;
    var user_domain = data.return_domain;
    var user_name = data.return_username;

    gotoAndPlay("finish");

    first_txt.text = first_nme;
    second_txt.text = second_nme;
    email_txt.text =  email_addr;
    username_txt.text = user_name;
    domain_txt.text = user_domain;


    }
    else {
        status_txt.text = "I hate flash";

    }

Now when i test that, flash prints out "I hate flash" in the status_txt field. So i then replace the value of status_txt to print out the variable that I'm using the if statements with (return_stat):

else {
            status_txt.text = return_stat;

        }

Then when i test it, it shows either PASSED or FAILED. Which means the issue does not lie in PHP as it's returning the correct data and the issue lies within the If statements.

I'm completely lost here. I don't see anything that I've done wrong, any ideas?

Thanks guys.

EDIT

My PHP CODE:

<?php

require ('installation_5_functions.php');
require ('cust_ver_i.php');


$username=$_POST['userName'];

$ident_encrypt=$_POST['userPsswrd'];


verify($reference_id, $username, $ident_encrypt);


if ($ref_id_stat == "FAILED"){

$retrn_stat = "FAILED";

print "return_value=$error_ref_id&return_stat_verify=$retrn_stat";  

exit();

}
if($ref_id_stat == "PASSED"){

if ($user_verify_status == "FAILED"){

$retrn_stat = "FAILED";


print "return_value=$user_verify_error&return_stat_verify=$retrn_stat";

}

elseif ($user_verify_status == "PASSED"){

if ($cust_status == "DEACT"){

$retrn_stat = "FAILED";

print "return_value=$display_error_stat&return_stat_verify=$retrn_stat";    

}
elseif ($cust_status == "ACTIVE"){

$retrn_stat = "PASSED"; 

print "return_first=$cust_first&return_second=$cust_last&return_email=$cust_email&return_username=$cust_username&return_domain=$cust_domain&return_stat_verify=$retrn_stat";

}

}
}



?>
  • 1
    What do you mean with `when I test it`? Obviously the value of `return_stat` is neither 'PASSED' nor 'FAILED'; more probably 'null' or 'undefined'. – RIAstar Sep 19 '11 at 10:34
  • I mean, when i view the swf from my server (in a browser). No, the return_stat DOES return the correct value of "FAILED" or "PASSED" as thats what I've told it to do in PHP. So, PHP returns a value for return_stat_verify as either "PASSED" or "FAILED" and flash then fetches that as a variables value (the value of return_stat). –  Sep 19 '11 at 11:17
  • and no, the value for return_stat is not "null" or "undefined".... as i stated in the question, I used an else statement to print the value of return_stat in the status_txt field which then printed the correct value ("PASSED" or "FAILED") –  Sep 19 '11 at 11:22
  • check this http://stackoverflow.com/questions/7333686/strange-issue-with-flash-php hope it helps. – hardik Sep 19 '11 at 11:26
  • you should use firebug to check return variable from php. – hardik Sep 19 '11 at 11:28
  • But i know the value is being returned correctly in PHP as i used an else statement in flash to print it out. –  Sep 19 '11 at 11:31
  • have you set this in flash urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES; – hardik Sep 19 '11 at 11:45
  • Edited the question with PHP code, no that I know why you need it. –  Sep 19 '11 at 11:54
  • trace your return values in flash and check it in flashlog.txt that resides at C:\Documents and Settings\[user name]\Application Data\Macromedia\Flash Player\Logs – hardik Sep 19 '11 at 11:55
  • and in flashlog.txt just notice is there any spaces (%20). – hardik Sep 19 '11 at 11:58
  • i do not have the directory you suggested. I've also done a whole computer search for flashlog.txt and cant seem to find it. –  Sep 19 '11 at 12:07
  • Application Data is a hidden folder. you need to change folder settings. – hardik Sep 19 '11 at 12:20
  • I have the folder AppData, no application data. Ive got show hidden folders active. I'm using Vista –  Sep 19 '11 at 12:21
  • check this path AppData\Roaming\Macromedia\Flash Player\Logs – hardik Sep 19 '11 at 12:26
  • and find mm.cfg file at C:\Users\\ and add this string (each sentence in new line) ErrorReportingEnable=0 TraceOutputFileEnable=1 MaxWarnings=3 SuppressDebuggerExceptionDialogs=1 – hardik Sep 19 '11 at 12:29
  • I get to the flash play folder and only 4 other folders appear. No logs. –  Sep 19 '11 at 12:39

4 Answers4

2

A lot of times there can be issues with whitespace. For example, if

data.return_stat_verify == 'FAILED  '

When you put it into a TextField, there will not appear to be a difference. To see if this is the case, you might try:

// place a character on both sides. That will show whether there is whitespace
status_txt.text = "|"+return_stat+"|";

EDIT

It looks like this is the case below. This means you need to trim your input. First, make sure that there is no extra whitespace in the PHP file (do you have the last line as ?> or is there an empty line at the end? An empty line, outside of ?> would cause the problem you describe.

Then, for safety's sake, check the input as well. Personally, I would use StringUtil.trim because it is the most explicit and because most languages have some way to do something very similar. Failing that, I would use regex to fix this:

// using two to be explicit
// /^\s+/m this removes all occurrences of one or more spaces at the beginning
return_stat = return_stat.replace(/^\s+/m, "");
// /\s+$/m this removes all occurrences of one or more spaces at the end
return_stat = return_stat.replace(/\s+$/m, "");

Of course, you could just in-line it:

// use the global flag if you are doing this all at once.
return_stat = return_stat.replace(/^\s+|\s+$/mg, "");
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • But I've check the PHP file. No spaces anywhere (where they shouldn't be). –  Sep 19 '11 at 11:19
  • Just tried that code. I got "|FAILED (and another | but in a new line.)" so the second | is under "|FAILED" –  Sep 19 '11 at 11:29
  • @Nav That's where your error is then: there's a newline character in the String ("FAILED\n"). Somehow in your php code you're adding a 'newline' to the variable. You could simply solve it by trimming `return_stat`. – RIAstar Sep 19 '11 at 12:25
  • okay, so what do you mean by trimming return_stat. how would i trim it? thanks. –  Sep 19 '11 at 12:36
  • @Nav Remove all the whitespace: `return_stat = return_stat.replace(/\s/m, "");`. `\s` means any whitespace character (including newline); `m` after the /.../ means 'do a _multiline_ search for this regular expression'. – RIAstar Sep 19 '11 at 13:19
  • @cwallenpoole or to make it even shorter with just one replace: `/^\s+|\s+$/gm` – RIAstar Sep 19 '11 at 14:46
0

Maybe you are not getting a String as return from the server? Try checking the type of the return.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#typeof

Mattias
  • 3,907
  • 4
  • 28
  • 50
0

Ok, I've managed to solve the problem. I placed exit(); at the bottom of every if/else statement in the php script and it seems to be working now. Makes no sense what so ever but it did the trick.

Thanks for your help.

  • It broke because your logic was off. Please if you are doing multiple comparisons where only one can be true. Use the switch(true) statement and put your comparisons on the case line "case (return_stat == "FAILED"):" Much more understandable code and you would never had this issue. – The_asMan Sep 19 '11 at 15:41
0

The problem could be caused by the closing php tag: ?> See: Why would one omit the close tag?

Any whitespace after that tag would be appended to any print/echo output. While additional whitespace in a html page wouldn't cause trouble, in your case it's a problem. It is easy to miss that, so it's best to avoid using the closing tag in those cases or to really make sure there aren't any characters after the tag.

Community
  • 1
  • 1
kapex
  • 28,903
  • 6
  • 107
  • 121
  • hey. But how could i avoid the php closing tag, as the code would not run then. or would it? –  Sep 19 '11 at 17:12
  • @Nav It runs fine without it. See the note of the [PHP Manual](http://php.net/manual/en/language.basic-syntax.instruction-separation.php): "The closing tag of a PHP block at the end of a file is optional [...]" – kapex Sep 19 '11 at 17:33