3 Replies Latest reply on May 13, 2006 2:55 AM by samdl1

    Simple(?) problems with "IF" statements and variables! Please help...

    Brett_Archibald
      Nested a couple of layers down into my movie, I've got a form with 2 blank fields and a checkbox, and a Submit button (the Submit button actually just plays a clip rather than any actual server-side submitting).

      What I WANT to have happen is for the clip NOT to play unless ALL 3 elements have been "utilised", ie: BOTH the fields to have SOME copy in and for the checkbox to be ticked.
      If ALL 3 criteria HAVE been met, then the clip should play.

      Before we get to this form, 3 variables are set further back in the timeline, up a couple of levels:
      step4a = "";
      step4b = "";
      step4e = "off";


      Back in my form, the 2 blank fields have "Var"s called step4aName and step4bEmail.
      Clicking the checkbox button sets the variable step4e to "on"

      The following code is attached to the Submit button:
      on (release) {
      // FIRST VARIABLE IS SET TO MATCH FIRST FIELD
      _parent._parent.step4a = step4aName;
      // SECOND VARIABLE IS SET TO MATCH SECOND FIELD
      _parent._parent.step4b = step4bEmail;
      // TRACE COMMANDS FOR ERROR CHECKING
      trace ("step4a = " + _parent._parent.step4a);
      trace ("step4b = " + _parent._parent.step4b);
      trace ("step4e = " + _parent._parent.step4e);
      // ONLY PLAY CLIP IF BOTH FIELDS ARE FILLED AND BOX IS TICKED
      if ((_parent._parent.step4a != "") &&
      (_parent._parent.step4b != "") &&
      (_parent._parent.step4e == "on")) {
      this.play ();
      }
      }


      When I test the movie, I get the following output (which is what I would expect, implying all is well):
      step4a =
      step4b =
      step4e = off


      Now what happens when I run this is that the clip will, quite correctly, NOT play if I just hit Submit without doing anything else first. But the problem comes when I ONLY tick the checkbox and then hit Submit, because then the movie clip DOES play, when it is NOT supposed to. Why? My two fields ARE still blank, as is proven by the trace commands I can see outputted when I hit the Submit button – they still say:
      step4a =
      step4b =


      To further complicate things, if I DO type something into the 2 fields, but I then delete what I've typed, then the movie clip will correctly NOT play (with the checkbox ticked). The two variables outputted still say:
      step4a =
      step4b =

      (And if I type some text into the 2 text fields, then the 2 traced variables outputted DO update to contain text that matches what I type in.)

      So according to the output I'm seeing, the way I've put it together SHOULD work, but it doesn't.

      So to sum up, the problem is, if I LEAVE my 2 fields totally blank, as it first appears on screen, then my error-checking doesn't work.
      If I type something in my fields and then MAKE them blank by deleting the text in them, then my error-checking works as it should.
      What's up with that?
        • 1. Re: Simple(?) problems with "IF" statements and variables! Please help...
          samdl1
          Hi Brett, I'm not sure what version of flash/as (this only applies to later versions) you are using but you might firstly consider placing this code in a keyframe on your timeline and using
          pathto.button.onRelease = function() {
          //code
          }
          and changing your fields to a similar syntax.

          The possible reason your error checking isn't working is because NULL values and "" values are different...when the textbox first loads the value is NULL as no value has been entered. The process of adding text and removing it leaves a value of "" - ie an empty string which fulfills your conditions.

          to solve the problem you could add NULL to your condition, eg

          if (((_parent._parent.step4a != "") OR (_parent._parent.step4a != NULL))
          &&
          ((_parent._parent.step4b != "") OR (_parent._parent.step4b != NULL))
          &&
          (_parent._parent.step4e == "on"))

          or of course if you are using a later version of flash simply test for

          my_Text.length > 0

          I hope this helps

          Sam
          • 2. Re: Simple(?) problems with "IF" statements and variables! Please help...
            Brett_Archibald Level 1
            Oh thank-you, thank-you, thank-you!!! I was tearing my hair out for ages over that one. And no wonder I couldn't solve it, as I had no idea that my value was coming out as NULL initially (wouldn't it be more helpful if the output window showed the value as NULL rather than just showing it as blank?)...

            Anyway, I just opted for your second simpler solution of my_Text.length > 0, and that worked great!

            On a separate note, what was that
            pathto.button.onRelease = function() {
            //code
            }

            all about? Is there any benefit to that? If not, I actually prefer to have my code ON my buttons, so that if, at a later date I need to see what a particular button does, then I can just click on the button and see the code I'm after, rather than having to go through several steps of finding the button, checking the function name, and then having to find where the function itself is. But if there's benefits to doing it the other way...?
            • 3. Re: Simple(?) problems with "IF" statements and variables! Please help...
              samdl1 Level 1
              Macromedia suggests as much code as possible is in one place - either in a .as file embeded in the swf or in frame one of the main timeline. The advantage of this method is that for anything other than the smallest of applications finding code on keyframes inside movieclips or attached to different buttons can be a real chore whereas if it is all in one place sections of code are easier to find (well actually more difficult to lose in my opinion). It is personal preference though so its really up to you. More info is available on flash help under coding best practices.


              Sam

              ps glad I could help