wongadob Posted November 21, 2011 Share Posted November 21, 2011 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 comment Share on other sites More sharing options...
Ingolme Posted November 21, 2011 Share Posted November 21, 2011 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 comment Share on other sites More sharing options...
wongadob Posted November 21, 2011 Author Share Posted November 21, 2011 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 ! Link to comment Share on other sites More sharing options...
ShadowMage Posted November 21, 2011 Share Posted November 21, 2011 The value of a checkbox never changes (unless you change it using elem.value). You need to check its checked property. if (test.checked) { Link to comment Share on other sites More sharing options...
wongadob Posted November 21, 2011 Author Share Posted November 21, 2011 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 comment Share on other sites More sharing options...
ShadowMage Posted November 21, 2011 Share Posted November 21, 2011 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 comment Share on other sites More sharing options...
Ingolme Posted November 21, 2011 Share Posted November 21, 2011 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 comment Share on other sites More sharing options...
wongadob Posted November 23, 2011 Author Share Posted November 23, 2011 the test.checked bit did what I needed. Thanks. All I needed to know was that at leasy one survey had been selected. I do all the processing of the actual data in PHP. Thanks again it is sorted! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.