Jump to content

Accessing Form Arrays In Javascript


Recommended Posts

I am having problems with getting the right syntax in Javascript to access and array set up in a form (for check boxes) via getElementById The setup on the form works fine because I can access it in PHP no problem. But can't get the syntax right in JS setup in PHP as followes

  while($row = mysql_fetch_row($result))  {   print "<input type ='checkbox' class = 'selectedsurvey' id = 'SelectedSurveys[".$x."]' name = 'SelectedSurveys[".$x."]' checked='checked'/>";   print "<label>$row[1]</label>";   print "<br/>\n";   $x+=1;  }

They are read from a database of surveys My code in JS

for(i=0;i<numberofsurveys;i++){  test = document.getElementById("SelectedSurveys"[i]);  if (test.value == "on")  {   errmsg = errmsg + "'" + test.value + "',";   anychecked = true;  } }if (!anychecked){  errmsg=errmsg+"Error: A site must contain at least 1 survey.<br/>"; }

Any help much appreciated. PS test is already defined in a var earlier.

Link to post
Share on other sites

The ID can't be an array. And I don't know if the ID attribute allows square brackets according the the specification. Anyways, given that it does allow square brackets, they're just part of the string: document.getElementById( "SelectedSurveys[" + i + "]");

Link to post
Share on other sites

So OK I changed the code as follows

for(i=0;i<numberofsurveys;i++){test = document.getElementById("SelectedSurveys["+i+"]");if (test.value == "on"){anychecked = anychecked + 1;}}

The return values I am getting are ALL set to "on". I even doubted my Logic code so instead of using true & false I decided just to add one to anycheked (it is now initialised with =0) then it tests if anychecked = 0 to determine if non are selected. However anychecked is always 25 (which happens to be the number of checkboxes) no matter how many you select and deselect. and as I said text.value is always = "on". Please help !

Edited by wongadob
Link to post
Share on other sites

The value of a checkbox never changes (unless you change it using elem.value). You need to check its checked property.

if (test.checked) {

Edited by ShadowMage
Link to post
Share on other sites

Sorry Shadowmage I did notunderstand that? I am accessing them fine in PHP via this code.

   $checked=false;   for($i=0;$i<$SVCount;$i++)		   {   foreach ($_POST["SelectedSurveys"] as $key => $value)	    {    if ((int)$key == $i)			      {				 $SelectedSurveys[$x]=$value;			    ;		   				 $checked=true;    }   }	      if($checked==false)	      {   		  $x++;	      }	      else	      {		  $checked=false;	   		  $x++;	      }  }		     

I noted that the only items posted are the ones that are checked. But it stores the key. Does Javascript work in a similar way. If so how could I access that info if it is not via the .value attribute?

Link to post
Share on other sites

When you write:<input type='checkbox' value='on' /> Its value will always be equal to 'on' whether it's checked or not. In order to see if it was checked, you have to look at its checked property. So your loop would look like this:

for(i=0;i<numberofsurveys;i++) {   test = document.getElementById("SelectedSurveys["+i+"]");   if (test.checked) {	  anychecked = anychecked + 1;   }}

Link to post
Share on other sites

Javascript isn't anything like PHP. When you ask it to check the value, it will give you the value property of the element, whether or not the element is selected. Javascript doesn't have any real form handling, all it does is manipulate the HTML elements of the page, some of which can be in a form

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...