rn14 Posted November 30, 2008 Share Posted November 30, 2008 I have 7 check boxes of which 5 and only 5 can be selected. These are printed in an array. These are my functions: <script type="text/javascript"> function checkArray(mainform, arrayName) { var retval = new Array(); for(var i=0; i < form.elements.length; i++) { var el = form.elements[i]; if(el.type == "checkbox" && el.name == arrayName && el.checked) { retval.push(el.value); } } return retval; } function checkForm(mainform) { var itemsChecked = checkArray(mainform, "meal[]"); alert("You selected " + itemsChecked.length + " items"); if(itemsChecked.length > 0) { alert("The items selected were:\n\t" + itemsChecked); } return false; } </script> This is the form that i use <form method="post" action = "our_menustestw2.php" name = "mainform" onsubmit="checkForm();" > <input type="checkbox" name="meal2[]" value="<?php echo $row['id']; ?>" CHECKED/> <input type="submit" value="Submit Changes" name = "b1"> Thanks in advance for any help Link to comment https://forums.phpfreaks.com/topic/134873-how-come-this-doesnt-work/ Share on other sites More sharing options...
rhodesa Posted November 30, 2008 Share Posted November 30, 2008 i only see one checkbox in your form, and it doesn't match the name you pass Link to comment https://forums.phpfreaks.com/topic/134873-how-come-this-doesnt-work/#findComment-702265 Share on other sites More sharing options...
rhodesa Posted November 30, 2008 Share Posted November 30, 2008 assuming your form just didn't copy wrong...i found a couple other issues. #1) In the form tag, to prevent the form from posting, you need to RETURN the return value #2) In checkArray, you start with mainForm, but then start using form instead #3) In checkForm, you use 'meal[]', but in your form, it is 'meal2[]' updated code that works: <script type="text/javascript"> function checkArray(mainform, arrayName){ var retval = new Array(); for(var i=0; i < mainform.elements.length; i++){ var el = mainform.elements[i]; if(el.type == "checkbox" && el.name == arrayName && el.checked){ retval.push(el.value); } } return retval; } function checkForm(mainform){ var itemsChecked = checkArray(mainform, "meal[]"); alert("You selected " + itemsChecked.length + " items"); if(itemsChecked.length > 0) { alert("The items selected were:\n\t" + itemsChecked); } return false; } </script> <form method="post" action = "our_menustestw2.php" name = "mainform" onsubmit="return checkForm(this);" > <input type="checkbox" name="meal[]" value="1" /> <input type="checkbox" name="meal[]" value="2" /> <input type="checkbox" name="meal[]" value="3" /> <input type="checkbox" name="meal[]" value="4" /> <input type="checkbox" name="meal[]" value="5" /> <input type="checkbox" name="meal[]" value="6" /> <input type="checkbox" name="meal[]" value="7" /> <input type="submit" value="Submit Changes" name = "b1"> </form> Link to comment https://forums.phpfreaks.com/topic/134873-how-come-this-doesnt-work/#findComment-702267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.