lcy Posted September 10, 2009 Share Posted September 10, 2009 Hi there. Hope to get some help with checkbox from you guys...thanks in advance. i have this as my checkboxes: <?php while($row = mysql_fetch_array($result)) { > <input type="checkbox" name="company_code[]" value="<?php echo "".$row['company_code']."";?>"> <?php echo $row['company_code']; ?><br><?php } ?> Now I'm having three problems here: i. to validate if no checkbox is checked ii.to apply the check all and uncheck all function iii.after the checkboxes are checked and the button submit is checked, the page return to itself. Here i need the checked checkbos to be remained checked. How can i do all these with the checkbox i'm having. Appreciate any help given. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/173748-solved-some-problems-with-checkbox/ Share on other sites More sharing options...
StefanRSA Posted September 10, 2009 Share Posted September 10, 2009 You can validate it with JS on submit by doing the following: function checkform ( form ) { select = -1; for (i=form.company_code.length-1; i > -1; i--) { if (form.company_code[i].checked) { select = i; i = -1; } } if (select == -1) { alert("You must select something") return false; } return true ; } Then Query your db for the values selected after insert and echo accordingly Quote Link to comment https://forums.phpfreaks.com/topic/173748-solved-some-problems-with-checkbox/#findComment-915914 Share on other sites More sharing options...
ignace Posted September 10, 2009 Share Posted September 10, 2009 When you submit a checkbox it will send the value in the attribute value if and only if it is checked. <?php while ($row = mysql_fetch_assoc($result)): ?> <input type="checkbox" name="company_code[]" value="<?php print $row['company_code']; ?>" <?php ((isset($_POST['company_code']) && in_array($row['company_code'], $_POST['company_code'])) ? ' checked="checked"' : ''); ?>> <?php print $row['company_code']; ?> <?php endwhile; ?> Quote Link to comment https://forums.phpfreaks.com/topic/173748-solved-some-problems-with-checkbox/#findComment-915919 Share on other sites More sharing options...
lcy Posted September 12, 2009 Author Share Posted September 12, 2009 Thanks for the solutions provided. I tried but they doesn't work. For the remained checking the checked checkbox, i've try another way and it's working. <?php while($row = mysql_fetch_array($result)){ ?> <input type="checkbox" name="company_code[]" value="<?php echo "".$row['company_code'].""; ?>" <?php if($company_code!=NULL){foreach ($company_code as $company_code_ticked) {if($company_code_ticked == $row['company_code']){ echo "checked";}}}?>> <?php echo $row['company_code']; ?><br> <?php } ?> Is there any other way to solve my first and second problems? Anyway, appreciate solutions provided. Quote Link to comment https://forums.phpfreaks.com/topic/173748-solved-some-problems-with-checkbox/#findComment-917007 Share on other sites More sharing options...
lcy Posted September 12, 2009 Author Share Posted September 12, 2009 I've got the solution for the first problem... function validateCB(theName){ var counter=0; var cb=document.getElementsByName(theName) for (i=0; i<cb.length; i++) { if((cb.tagName=='INPUT')&&(cb.type=='checkbox')){ if (cb.checked) counter++; } } if (counter==0) { return false; } return true; } if (!validateCB('company_code[]')) { alert("Please select company code"); return false; } If anyone is having the solution for checking and unchecking all checkboxes, help is much appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/173748-solved-some-problems-with-checkbox/#findComment-917040 Share on other sites More sharing options...
lcy Posted September 12, 2009 Author Share Posted September 12, 2009 i've solve my problem...Code that i used is from site below: http://codingforums.com/showthread.php?t=47082 Quote Link to comment https://forums.phpfreaks.com/topic/173748-solved-some-problems-with-checkbox/#findComment-917094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.