passing condition

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

    #16
    Hello Bytes:

    Here's a continuation of my dilema :)
    Now it works fine but Im planning to put more functionality on my JS script so Im planning to have a 'header JS' to call the subs functions. The problem is that it do call the the sub function but it let the browser go to the next page even though the condition didn't pass to return true. Please help. Thanks in advance!

    The button Form
    Code:
    <form method="post" name="recapform" action="tblevent_entry_recap_handler.php?id=<?php echo $_GET['id'];?> " onsubmit="return show_alert()">
    header function
    Code:
    <script type="text/javascript">
    function show_alert()
    {
    entity_alert();
    
    }
    </script>
    sub function
    Code:
    <script type="text/javascript">
    function entity_alert()
    {
    x=  parseInt(document.getElementById('Organization').value)+parseInt(document.getElementById('Agencies').value)
    	+parseInt(document.getElementById('Education').value)+parseInt(document.getElementById('Medical').value)+parseInt(document.getElementById('Business').value)
    	+parseInt(document.getElementById('Professional').value)+parseInt(document.getElementById('Government').value)+parseInt(document.getElementById('Residential').value)
    	+parseInt(document.getElementById('ResidentialFacility').value);
    	
    y = parseInt(document.getElementById('ActualAttendees').value)
    
    if (x>y)
      {
    	alert ('Entity is over by:' + (x-y));
    	return false;
      }
    else
      {
    	     return true;
      }
    
    }
    </script>

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #17
      In show_alert(), add a return:
      Code:
      [i]return[/i] entity_alert();

      Comment

      • ddtpmyra
        Contributor
        • Jun 2008
        • 333

        #18
        It works but when I added more function underneath in only perform the most top functionalities ... am i doint this correctly?

        header function
        Code:
        <script type="text/javascript">
        function show_alert()
        {
        return entity_alert();
        return age_alert();
        
        
        }
        </script>
        sub functions
        Code:
        function entity_alert()
        {
        x=  parseInt(document.getElementById('Organization').value)+parseInt(document.getElementById('Agencies').value)
        	+parseInt(document.getElementById('Education').value)+parseInt(document.getElementById('Medical').value)+parseInt(document.getElementById('Business').value)
        	+parseInt(document.getElementById('Professional').value)+parseInt(document.getElementById('Government').value)+parseInt(document.getElementById('Residential').value)
        	+parseInt(document.getElementById('ResidentialFacility').value);
        	
        y = parseInt(document.getElementById('ActualAttendees').value)
        
        if (x>y)
          {
        	alert ('Entity is over by:' + (x-y));
        	document.getElementById('Organization').focus();
        	return false;
          }
        else
          {
        	     return true;
          }
        
        }
        </script>
        
        
        <!--- 2nd function -->
        <script type="text/javascript">
        function age_alert()
        {
        tAge=  parseInt(document.getElementById('Under18').value)+parseInt(document.getElementById('Eighteen34').value)+parseInt(document.getElementById('ThirtyFive54').value)+parseInt(document.getElementById('FiftyFour').value);
        				
        a = parseInt(document.getElementById('ActualAttendees').value)
        			
        if (tAge>a)
         {
        alert ('Age Group is over by:' + (tAge-a));
        document.getElementById('Under18').focus();
        return false;
          }
        else
         {
         return true;
          }
        			
        }
        </script>

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #19
          Once you return, it won't execute any more statements.

          Try something like:
          Code:
          return (funA() && funB());

          Comment

          • ddtpmyra
            Contributor
            • Jun 2008
            • 333

            #20
            Originally posted by acoder
            Once you return, it won't execute any more statements.

            Try something like:
            Code:
            return (funA() && funB());
            Here's what I did below but it only performs the first function. Anything do i have to do on sub functions?

            Header
            Code:
            <script type="text/javascript">
            function show_alert()
            {
            return (age_alert() && entity_alert());
            }
            </script>
            sub two functions :
            1).
            Code:
            <script type="text/javascript">
            function entity_alert()
            {
            		tEntity=  parseInt(document.getElementById('Organization').value)+parseInt(document.getElementById('Agencies').value)
            			+parseInt(document.getElementById('Education').value)+parseInt(document.getElementById('Medical').value)+parseInt(document.getElementById('Business').value)
            			+parseInt(document.getElementById('Professional').value)+parseInt(document.getElementById('Government').value)+parseInt(document.getElementById('Residential').value)
            			+parseInt(document.getElementById('ResidentialFacility').value);
            			
            		y = parseInt(document.getElementById('ActualAttendees').value);
            		
            	if (tEntity>y)
            		  {
            			alert ('Entity is over by:' + (tEntity-y));
            				return false;
            			  }
            				else	
            			  {
            				 return true;
            			  }
            }
            </script>
            2).

            Code:
            <script type="text/javascript">
            function age_alert()
            {
            		y = parseInt(document.getElementById('ActualAttendees').value);
            		tAge=  parseInt(document.getElementById('Under18').value)+parseInt(document.getElementById('Eighteen34').value)+parseInt(document.getElementById('ThirtyFive54').value)+parseInt(document.getElementById('FiftyFour').value);
            	if (tAge>y) {
            				alert ('Age Group is over by:' + (tAge-y));
            				return false;
            			  	}
            	
            	else     	{
            				 return true;
            				}
            }
            </script>

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #21
              If age_alert() returns false, it won't go to entity_alert() because there's no need.

              Comment

              • ddtpmyra
                Contributor
                • Jun 2008
                • 333

                #22
                Originally posted by acoder
                If age_alert() returns false, it won't go to entity_alert() because there's no need.
                Please tell me what the best way to handle this two conditions before it let the user to go to the next page?

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #23
                  Find a scenario when the first function should return true and the second false to see if it works.

                  Comment

                  • ddtpmyra
                    Contributor
                    • Jun 2008
                    • 333

                    #24
                    I just redo it and make it simplier and it works perfectly fine. Thanks for all of your inputs ACECODE

                    Code:
                    	
                    if  ((a<=y) && (b<=y) && (c <=y) && (d<=y)  && (Ustatus=='Complete'))
                     {   
                    return true; 
                      }
                    else {   alert ('Please check your entry total and event status');
                    return false;
                    }

                    Comment

                    Working...