Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jas123

macrumors member
Original poster
Apr 1, 2008
97
0
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?
 
the return false statement doesn't execute. It will only work if I place it inside the for loop.

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.
 
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>
 
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.
 
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...
 
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 =.
 
Seems like the loop went out of bound:

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

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.
 
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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.