A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [RESOLVED] The variables returned to Flash from a PHP script DEFY the If-Then loops in flash.

  1. #1
    Member
    Join Date
    Aug 2014
    Posts
    51

    resolved [RESOLVED] The variables returned to Flash from a PHP script DEFY the If-Then loops in flash.

    Hi to all gurus,

    Here is a small program in flash which calls values from PHP and displays them correctly.

    Code:
    path = "http://localhost/xampp/nwjv/php/"; //declare path to php files
    lvOut = new LoadVars(); //create lv object sending variables OUT to php
    lvIn = new LoadVars(); //create lv object receiving variables IN from php
    
    
    lvIn.onLoad = function (success) {
    if(success)
    {
    //PHP variable value to textbox
    
    InVal = lvIn.returnVal;
    InTxt = lvIn.retVal;
    
    output.text = InVal;
    output1.text = InTxt;
    
    
    /* output1.text = "No Value";
    
    if(InTxt == 'lo' )
    {
    output1.text = "Low Value";
    }
    
    if(InTxt == 'hi')
    {
    output1.text = "High Value";
    }
    */
    }else{
    //...or notify of failure
    output.text = "fail";
    }
    }
    
    
    
    myBtn.onRelease = function(){
    //assign user-input value to lv property called years
    lvOut.years = years.text;
    //send to a blank window
    // lvOut.send(path + "dogyears_new1.php",lvIn,"GET");
    lvOut.sendAndLoad(path + "dogyears_new1.php",lvIn,"GET");
    };
    And the simplest PHP code


    PHP Code:
    PHP Code:
    <?php$calculation = $_GET["years"]*2; 
    if($calculation <=10 ) $retVal="lo"; 
    if($calculation > 10)  $retVal="hi"; 
    echo "&returnVal=$calculation &retVal=$retVal" ;?>

    PHP returns two values which are collected by flash in variables InVal and InTxt. The values collected are correct and are displayed thus in the 2 output boxes.

    Now if i the commented out If then block is activated by removing the /* */ from around it and the program is run, clearly the if then blocks fail since the comparison of InTxt fails. It completely fails me why this is happening. While I can display the values correctly, I can't use them in conditional loops. I have even tried them in switch case statements with the same frustrating result. ( The output1.text remains equal to "No Value" when it should change to either "High Value " or "Low Value")

    Earlier I was using numbers and when that failed I tried to use strings since I thought that for some reason PHP returns everything as strings. Please note that the command typeof InTxt (not used in the above code) returns a String suggesting that InTxt is a string variable.

    But as can be seen, even the string comparison fails.

    Can someone please comment on this behavior and suggest a solution.
    Thanks loads to all on the forum for any help.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Try this
    PHP Code:
    //path = "http://localhost/xampp/nwjv/php/";//declare path to php files
    path "p.php";
    lvOut = new LoadVars();
    lvIn = new LoadVars();

    lvIn.onLoad = function(success)
    {
        if (
    success)
        {
            
    InVal lvIn.returnVal;
            
    InTxt lvIn.retVal;

            
    output.text InVal;
            
    output1.text InTxt;
        }
        else
        {
            
    output.text "fail";
        }
    };

    myBtn.onRelease = function()
    {
        
    lvOut.years years.text;
        
    lvOut.sendAndLoad(path,lvIn,"POST");
        
    //lvOut.sendAndLoad(path + "dogyears_new1.php",lvIn,"GET");
    }; 
    you can always make your path realative rather than absolute so it works everywhere you put it.

    php, I think you basically just needed the space after the first<?php.

    Change p.php to your filename, I just sued it for my testing purposes
    PHP Code:
    <?php 

    $calculation 
    $_POST["years"] * 2;
     
    if(
    $calculation <=10 )
    {
        
    $retVal="lo"
    }
    else
    {
        
    $retVal="hi";
    }

    echo 
    "&returnVal=".$calculation."&retVal=".$retVal;

    ?>

  3. #3
    Member
    Join Date
    Aug 2014
    Posts
    51
    Hi fruitbeard,

    Thanks for the reply. Really appreciate that. However, forgive me please, but I think you have misunderstood my question because in your code you have totally omitted the commented out portion with the IF - THEN block.

    The results that I get from php are just fine as far as displaying them is concerned. InTxt displays 'hi' and 'lo'. However it's when I use InTxt in an IF-Then block, the Intxt kind of defies the conditional tests. It is as if the conditional tests do not recognize the value of Intxt even though flash displays it correctly.

    Please find the relevant php and fla files for the above problem zipped in the attached file.

    Run those using values in Input < 5 first and then > 5. You will see the numeric results in the output box ( 2* Input) and you will see the 'hi' and 'lo' appear in the output1 box.

    Next un-comment the IF - THEN block and try again. You will see that the IF-THEN conditional is bypassed as if there was no value in InTxt and infact that's the default value that would be displayed in the output1 Box (No Value).

    Once again I really appreciate your help. I will try out the changes in php you suggested.

    Really looking forward to some reply, help, suggestion, solution to this problem.

    Thanks fruitbeard and all other guys here!
    Attached Files Attached Files

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Fla
    PHP Code:
    var path:String "http://localhost/xampp/nwjv/php/dogyears_new1.php";
    var 
    lvOut:LoadVars = new LoadVars();
    var 
    lvIn:LoadVars = new LoadVars();

    lvIn.onLoad = function(success)
    {
        if (
    success)
        {
            var 
    InVal:Number lvIn.returnVal;
            var 
    InTxt:String lvIn.retVal;

            
    output.text InVal;
            if (
    InTxt == 'lo')
            {
                
    output1.text "Low Value";
            }
            else if (
    InTxt == 'hi')
            {
                
    output1.text "High Value";
            }
        }
        else
        {
            
    output.text "fail";
        }
    };

    myBtn.onRelease = function()
    {
        
    lvOut.years years.text;
        
    trace(lvOut.years);
        
    lvOut.sendAndLoad(path,lvIn,"POST");
    }; 
    php
    PHP Code:
    <?php

    $calculation 
    $_POST["years"] * 2;

    if(
    $calculation <=10 )
    {
        
    $retVal="lo";
    }
    else
    {
        
    $retVal="hi";
    }

    echo 
    "&returnVal=".$calculation."&retVal=".$retVal."&";

    ?>
    you needed a trailing & sign to make the vars matchable.

    You could of course just have the php send the values instead of altering it inside of flash which would end the problem of adding the extra & sign too.

  5. #5
    Member
    Join Date
    Aug 2014
    Posts
    51
    Hi fruit beard,

    Thanks loads for that insight. Getting the variables out of the ""and appending values using the dot operator seems to have done it. Grateful to you for that vital pointer.

    I would be glad if you could explain the usage of the '&' at the end a little more detailed. Also I could not exactly make out your last statement where you said that it is possible to do away with the '&'. I would be glad if you could explain that with an example.

    Thanks again very much. I was stuck on this for almost 3 days.

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I believe that vars sent this way seem to have an extra space or something after them, so adding "&" at the end finishes it or cheats it into thinking there is another var, something along those lines, look it up.


    The way I did the php is just the way I prefer as you can automatically see any $stuff, the other way would work too.

    You can do it like this so you dont need to add the extra "&", just swap the order around.

    PHP Code:
    echo "&retVal=".$retVal."&returnVal=".$calculation
    Also you could just send the vars back to flash without changing them in flash, like so
    PHP Code:
    if($calculation <=10 )
    {
        
    $retVal="Low Value.";
    }
    else
    {
        
    $retVal="High Value.";


    save you fiddling around with flash.
    Last edited by fruitbeard; 08-12-2014 at 07:35 AM.

  7. #7
    Member
    Join Date
    Aug 2014
    Posts
    51
    Hi fruitbeard,

    Thanks loads for the explanation. Much obliged.

    The variables that I sent from php into flash and pass them thru a loop was just to check the working of the if-then loop.

    Thanks loads.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center