A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: IF/Then problem with ActionScript and PHP

  1. #1
    Keeper of Useless Information
    Join Date
    Feb 2006
    Posts
    19

    IF/Then problem with ActionScript and PHP

    Hello,

    I started a thread on the Newbies forum titled "Can't Receive from PHP using LoadVars, sendAndLoad and onLoad". DallasNYC solved a lot of the issue I was having with Flash and ActionScript. As my project advanced, it seems there is now an issue that he suggested I post to this forum.

    Since I'm new to Flash, I thought I'd start out simple with an email contact form. I have a Flash based form that collects name, email and message. When you click submit, if the email address the visitor typed is valid, I get an email with all of the information and a "Your message has been sent" displays on the screen. If the address the visitor entered is not valid, it displays a "Your address is not valid" message on the screen. Everything works up to the message displaying on the screen part. I get the email, but the message is never displayed.

    Here's the code that pertains to the messages:
    Code:
    dataHolder.onLoad = function(success:Boolean) 
    {
       if (success) 
       {
          if (dataHolder.response == "passed") 
          {
             outgoing_txt.text = "Your message was sent. "+dataHolder.response;
          } else if (dataHolder.response == "error") {
             outgoing_txt.text = "Your email address does not appear valid."+dataHolder.response;
          } else {
             outgoing_txt.text = "PHP page returned something. "+dataHolder.response;
          }
       } else {
          outgoing_txt.text = "Connection with server failed. "+dataHolder.response;
       }
    }
    If I enter a valid email address what appears on the screen is "PHP page returned something. passed". If I enter an invalid email address, what appears on the screen is "PHP page returned something. error".

    It seems that what's being returned is not passing the IF tests. Although, the correct condition is being returned (passed or error). I've tried using PHP trim() in case there was empty spaced and/or unnecessary stuff being returned, but that did not help either.

    Anyone have any idea why this is happening, why ActionScript is not picking up on the condition correctly?

    Thanks,
    Digital Gypsy
    Last edited by DigitalGypsy; 02-28-2006 at 05:00 PM.

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    Can't see anything wrong in this code.

    If you've got the correct code to create the object and the sendAndLoad()....
    code:

    var dataHolder = new LoadVars()
    dataHolder.onLoad = function(success:Boolean)
    {
    if (success)
    {
    if (this.response == "passed")
    {
    outgoing_txt.text = "Your message was sent. "+dataHolder.response;
    } else if (this.response == "error") {
    outgoing_txt.text = "Your email address does not appear valid."+dataHolder.response;
    } else {
    outgoing_txt.text = "PHP page returned something. "+dataHolder.response;
    }
    } else {
    outgoing_txt.text = "Connection with server failed. "+dataHolder.response;
    }
    }
    dataHolder.sendAndLoad(url, dataHolder, "POST");


    I prefere using this inside the method to refer to the object, but it's the same.

    If you trace:
    code:

    trace (this.response);


    inside the onLoad() what do you get?

    If you want to make sure there are no blank spaces, you can trace something like:
    code:

    trace ("***" + this.response + "***");


  3. #3
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Yep as nunomira says ..code looks fine.. if you like u can download my flash/php feedback form from http://www.flashmatics.co.uk/blog/?p=7 and check out and compare the code, to figure out why urs is not working
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  4. #4
    Keeper of Useless Information
    Join Date
    Feb 2006
    Posts
    19
    Hello,

    I added the trace() function like this:
    trace ("***" + dataHolder.response + "***");

    That's a nice function to know

    When I entered a valid email address, I got this in return:

    ***passed

    ***

    When I enter an invalid address I get this:

    ***error

    ***


    For some reason there seems to be a return and a blank line inserted in the data coming back from php.

    My php looks like this:
    PHP Code:
    switch ($valid)
    {
       case 
    0:
          
    mail($toaddress,$subject,$message,"From: $name <$email>");
          echo 
    trim('response=passed');
          break;
       case 
    1:
          echo 
    trim('response=error');
          break;
    }
    //end switch 
    I also tried the PHP without the trim() funciton and I still get the same results. I'm going to try it on another server to see if the same thing happens.

    What would make this occur? Anyone else ever seen this?

    Thanks,
    Digital Gypsy

  5. #5
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    That means that you're echoing a lot of white space from php.
    You should send the variables with ampersands around them. This ensures that there is no blank space.

    echo trim('&response=error&');

  6. #6
    Keeper of Useless Information
    Join Date
    Feb 2006
    Posts
    19

    Solved!

    Hello,

    That did the trick!

    I got this response:
    Your message was sent.

    Anyone care to comment on why I'm getting all of those extra artifacts from PHP?

    Thanks,
    Digital Gypsy

  7. #7
    Senior Member
    Join Date
    Oct 2003
    Posts
    1,354
    I know there is an ignoreWhiteSpace for XML. Is there a trim white space function in flash? And/Or is there an easier way to write this?
    Code:
    function trim(myString:String) {
    	trace(myString);
    	myString = escape(myString);
    	i = 0;
    	while (i<myString.length) {
    		trace(i+" : "+myString);
    		if (myString.substr(i, 1) == "%") {
    			trace("Changed:");
    			myString = myString.substring(0, i)+myString.substring(i+3, myString.length);
    			i -= 1;
    		}
    		i += 1;
    	}
    	trace("Final = "+myString);
    	return (myString);
    }
    testVar = " "+"pas sed"+"  "+newline+"  ";
    if (testVar == "passed") {
    	trace("First try PASSED");
    } else {
    	trace("First try FAILED");
    }
    testVar = trim(testVar);
    if (testVar == "passed") {
    	trace("Second try PASSED");
    } else {
    	trace("Second try FAILED");
    }
    The basic idea is to escape everything, then delete any of the string values that begin with a '%' plus the two characters that follow. I realize this actually strips all special characters, but I don't know what other characters are considered white space besides space and newline?
    Last edited by DallasNYC; 02-28-2006 at 10:29 PM.

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