? about javascript/php functions and nested loops

Discussion in 'Web Design and Development' started by Jas123, Sep 20, 2010.

  1. Jas123 macrumors member

    Joined:
    Apr 1, 2008
    #1
    In Javascript or PHP, when a loop inside of a function breaks, does it return the function as well?

    for instance, In javascript:

    function scan() {

    for (var i = 0; i <= document.register.length; i++){

    }
    return false;

    }

    the return false statement doesn't execute. It will only work if I place it inside the for loop.

    I am returning false to prevent a form from being submitted.

    Does PHP behave like this as well?
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    That's not my experience. I don't see anything here that would lead me to believe the return false part won't execute. I've done similar things many times.

    You might need a better example to get your point across. Also be sure to use the code block around your code (the button with a # on it or the PHP one) so that your coded is shown formatted here.
     
  3. Jas123 thread starter macrumors member

    Joined:
    Apr 1, 2008
    #3
    Here's the code.

    Code:
    	
    <form name="register" onsubmit="return scan()" action ="">
    		<div id="something">
    		<input type="radio" name="radioButton" checked="checked" value="yes" id="yes" >yes<br />
    		<input type="radio" name="radioButton" value="no" id="no">no<br />
    		</div>
    		<div id="error"></div>
    		<input type="submit" name="submit" value="submit">
    </form>
    </body>
    
    <script type="text/javascript">
    
    	function scan() {
    	
    	 var error = document.getElementById("error");
    
    			for (var i = 0; i <= document.register.length; i++){
    				
    						
         			if (document.register[i].parentNode.id == "something"){   
    				  error.innerHTML = "<p>Yes</p>";
    					 
    				
    				} else {
    				  error.innerHTML = "<p>No</p>";
    					  
    				}
    		 
    		
    		} 
    
        	return false;	
    	}
    
    </script>
    
    
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    My guess would be that you're getting an error during the for loop. Not on the first loop, but eventually, before it is done. This bit "document.register.parentNode.id" is possibly becoming null, which would cause it to error out and the function would never finish, which means it would not return "false" at the en of your function. Open the browser's error console to see any error messages when you try to submit the page to check for errors.

    Also, working through the document.register array isn't a very good way to go through form fields. You should call them out specifically.
     
  5. HomeBru Studios macrumors member

    HomeBru Studios

    Joined:
    Jun 4, 2008
    #5
    From what I see in your code, it will run the entire 'for' loop, constantly overwriting the error.innerHTML with the appropriate value for the current iteration of the loop. The value of the error.innerHTL when the loop finishes will be whatever the very last iteration sets it to. When the all iterations of the loop have run, THEN it will return FALSE.

    It's hard to tell what your real intention with this code is supposed to be...
     
  6. Dunmail macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #6
    I think that the intention is either to find the first error in the array or to print the error text for each error found.

    If the first then the OP needs a break statement after setting the text. If the latter then appending the text rather than reassigning it is the way to go, i.e += rather than =.
     
  7. stndn macrumors member

    Joined:
    Oct 22, 2006
    Location:
    earth
    #7
    Seems like the loop went out of bound:

    Try < instead of <=


    To be on the safe side, you could also do some check for the existence of document.register[ i ] before working further on it.


    -stndn.
     
  8. colocolo macrumors 6502

    Joined:
    Jan 17, 2002
    Location:
    Santiago, Chile
    #8
    As stated above, the mistake lies in i <= instead of just < .
    This will happen every time you loop through the length of an array, in any language that begins arrays at 0 (most of them nowadays).

    The reason is pretty straightforward; if you have an array A which contains 3 elements, the elements will be placed in 0,1, and 2. Thus, when looping through the length (3), you must stop at length-1(2).
     
  9. Jas123 thread starter macrumors member

    Joined:
    Apr 1, 2008

Share This Page