passing condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    passing condition

    on my function script i wanted to direct my page into another when it passes my condition.
    Code:
    <script type="text/javascript">
    
    function show_alert()
    { 
    if (x > y)
      {
    	alert ('value is okay');
    	// what code to place here when i want to direct it to the next page?
      }
    }
    </script>
    
    <form method="post">
    <input type="submit" onclick="show_alert()" />
    </form>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    you may use:

    Code:
    window.location.href = 'whateverUrl';
    kind regards

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Alternative:

      Since this is going to be called via a form submit, set the form action and then return true when the condition is met, and false otherwise. That way, this will work even if JavaScript is disabled.

      Comment

      • ddtpmyra
        Contributor
        • Jun 2008
        • 333

        #4
        Originally posted by acoder
        Alternative:

        Since this is going to be called via a form submit, set the form action and then return true when the condition is met, and false otherwise. That way, this will work even if JavaScript is disabled.

        any simple code you can share :)

        thanks!

        Comment

        • ddtpmyra
          Contributor
          • Jun 2008
          • 333

          #5
          Originally posted by gits
          you may use:

          Code:
          window.location.href = 'whateverUrl';
          kind regards
          hi Gits!
          this is not working :( this is what i did
          Code:
          if (x>y)
            {
          	alert (x-y);
            }
          else
            {
             window.location.href = 'home.php'; 
            }

          Comment

          • ddtpmyra
            Contributor
            • Jun 2008
            • 333

            #6
            Originally posted by acoder
            Alternative:

            Since this is going to be called via a form submit, set the form action and then return true when the condition is met, and false otherwise. That way, this will work even if JavaScript is disabled.
            Here what I have in placed
            Code:
            <form action="home.php" onsubmit="return validate_form()" method="post">
            JS
            Code:
            function validate_form()
            {
            x=   parseInt(document.getElementById('Organization').value)+parseInt(document.getElementById('Agencies').value);
            y = parseInt(document.getElementById('ActualAttendees').value)
            
            if (x>y)
              {
            	alert (x-y);
              }
            else
              {
               window.location.href = 'home.php'; 
              }
            
            }

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              what is the code exactly supposed to do? or better ... what exactly do you want to achieve?

              kind regards

              Comment

              • ddtpmyra
                Contributor
                • Jun 2008
                • 333

                #8
                If the x is less than y... I wanted to allow the user to view the page (or html)

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  then you should return false in the other case ...

                  Comment

                  • ddtpmyra
                    Contributor
                    • Jun 2008
                    • 333

                    #10
                    can you show here how?

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #11
                      put:
                      Code:
                      return false;
                      after your alert ...

                      kind regards

                      Comment

                      • ddtpmyra
                        Contributor
                        • Jun 2008
                        • 333

                        #12
                        It works! Thanks a lot!

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5388

                          #13
                          glad to hear that :)

                          Comment

                          • ddtpmyra
                            Contributor
                            • Jun 2008
                            • 333

                            #14
                            just a follow-up... now on my alert box i wanted to put string and variable value. I did this changes below but it doesnt take effect what else to do?
                            Code:
                            if (x>y)
                              {
                            	alert ('your are over by:' x-y);
                            	return false;
                              }
                            else
                              {
                            	 alert (x);
                                 return true;
                              }

                            Comment

                            • gits
                              Recognized Expert Moderator Expert
                              • May 2007
                              • 5388

                              #15
                              you need to do a operation with the values ...
                              Code:
                              alert ('your are over by: ' + (x-y));

                              Comment

                              Working...