problem w javascript for loop

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

problem w javascript for loop

Post by victor »

I have a function validator which make sure the user clicks on all the checkboxes prior to submit, however for loop doesn't seem to be actuated at all and nothing goes into it..

Code: Select all

<SCRIPT>
  function validate(f)
  &#123;
	  for(i=0;i<f.New_Sizes.length;i++)
  	  &#123;
  	   if(f.New_Sizes&#1111;i].checked==false)
  	   &#123;
  	     alert("In order to update, please click on all the checkboxes.");
  	     return false;
  	   &#125;
  	  &#125;
  &#125;
</SCRIPT>
any help is greatly app.

victor
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

what is f?
is it a refrence to the form or a refrence to the check box elements?

Alert(f) and what does it say.
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

onSubmit="return validate(document.updateSize) f is document.updateSize

alert f returns object. I can only alert f outside the for loop but not inside.
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

alert f this round returns undefined. My html form is written as follows

Code: Select all

<form name="updateSize" method="post" action="update_class_size.php" onSubmit="return validate(document.updateSize)">
i have a foreach loop which prints out a checkbox on every row, the name the each checkbox is a string variable randonly named like:

Code: Select all

echo "</td><td align=center><input type='checkbox' name='New_Sizes&#1111;]' value='".$string."'>";
any idea?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

what is wrong is that you have named your checkboxes as New_Sizes[] which javascript can't understand, it works well for passing all the values in the group of checkboxes to php as an array but javascript just gives up and dies.

the solution would be if you need to have those check boxes as an array when the form gets submitted would be to use ID tags like so.

Code: Select all

<?php
 echo "</td><td align=center><input type='checkbox' name='New_Sizes[]' id='NewSizes' value='".$string."'>";
?>
then in your js something like

Code: Select all

&lt;SCRIPT&gt;
function Validate(frm)
&#123;  
  var elements = frm.elements;
  var e = elements.length;
  for(var i=0; i&lt;e; i++)
  &#123;
     if(elements&#1111;i].type=='checkbox' &amp;&amp; elements&#1111;i].checked =='false'')
     alert("In order to update, please click on all the checkboxes.");
     return false; 
  &#123;
&#125;
&lt;/SCRIPT&gt;
something like that anyway should work for you.
Post Reply