)(Gravitek Posted February 20, 2007 Share Posted February 20, 2007 Hi there, I'm wanting to use a javascript form validation that alerts the user if none of the available checkboxes have been checked. I've done some surfing to try to find a solution, but I think the recommendations aren't working becuase of the way I've created the checkboxes... the following is the line of PHP code i use to display the checkbox in a table. The table is created from a loop because it obtains the information from a MySQL database (hence the use of echo): echo '\t<TD><CENTER><INPUT TYPE="CHECKBOX" NAME="del[]" VALUE=".$row['id']."></TD>\n'; After submission of the form, PHP deletes the record out of the MySQL database, using $row['id'] as the identifier, which is why i've used del[] as the NAME of the input type: if ($_POST['delete']) { $del = $_POST['del']; foreach ($del as $array) { $delsql = "DELETE FROM spares WHERE id='$array';"; mysql_query($delsql); echo "Record number ".$array." deleted!<BR>"; } } ---- from here on is where I am stuck ---- The idea I had to validate the form was to use a function. The function would be called when the submit button is pressed like this (the table NAME is deletetable): <INPUT TYPE="button" NAME="delete" onclick="validateCheckboxes()"> The function looks like this (which isnt working) function validateCheckboxes() { var formObj = document.deletetable; if (formObj.del.length == "0") { alert("Please select the records you wish to delete and try again!"); return false; } else { formObj.submit; } } Any help would be greatly appreciated!!!!! Link to comment https://forums.phpfreaks.com/topic/39278-detect-checkboxes/ Share on other sites More sharing options...
fenway Posted February 20, 2007 Share Posted February 20, 2007 Two things: first, the form element is called "del[]", not "del". Second, it's length will always be equal to the number of checkboxes; you'll have to examine the .checked attribute of each one in turn. Link to comment https://forums.phpfreaks.com/topic/39278-detect-checkboxes/#findComment-189424 Share on other sites More sharing options...
)(Gravitek Posted February 20, 2007 Author Share Posted February 20, 2007 Thanks for the reply. I must be doing something wrong still however, because I get syntax error on line 3... function valIssue() { var formObj=document.deletetable; var x=formObj.del[].length; for (var y = -1; y <= x; y++) { if (formObj.del[y].checked = TRUE) { var submit = "1"; } } if (submit == "1") { formObj.submit; } else { alert("Please select part(s) to delete!"); } } Link to comment https://forums.phpfreaks.com/topic/39278-detect-checkboxes/#findComment-189887 Share on other sites More sharing options...
fenway Posted February 21, 2007 Share Posted February 21, 2007 You'll have to write formOb.elements['del[]'].length to keep JS from trying to parse those square brackets; and you need to use the == oeprator on line 5, not the assignment operator. Link to comment https://forums.phpfreaks.com/topic/39278-detect-checkboxes/#findComment-190225 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.