how to limit validate max 3 check box which are fetch from database (dynamic) <table width="140" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="center" class="line"><input type="checkbox" name="ckb" value="<?php echo $relist['id']; ?>" onclick="chkcontrol(this.value)" /> <span class="compare"><a href="#">Compare</a></span></td> </tr></table> HTML:
Limiting check box is a Client side coding. So you need to execute JS to run it. And the problem with JS is anyone can control it via third party plugin. I may not have understood your question
I just repeat my question how to validate checkbox maximum select 3 checkbox when fourth checkbox is selected then show alert message <?php $sqllist="select subitem.id, subitem.subitem, subitem.shortdesc, subitem.image, subitem.bid, subitem.sid, subitem.model, subitem.sales, subitem.salesdesc, subitem.description, brand.brand ". "from subitem, brand ". "where subitem.bid = brand.primid and pid=$pid and itemid=$itemid order by id asc"; $pager = new PS_Pagination($conn, $sqllist, 10, 4, "pid=$pid&itemid=$itemid"); $rslist = $pager->paginate(); $k=0; while(@$relist=mysql_fetch_array($rslist)) { ?> <input type="checkbox" name="ckb" value="<?php echo $relist['id']; ?>" onclick="chkcontrol(this.value)" /> <?php } ?> PHP:
don't this validate? chkcontrol(this.value) just check if the total value ==3 then alert and then set the value = 0 for the "this" check box. So you must need to send "this" not only the "this.value" to restore the value
I trying javascript function but It's not working like this <script type="text/javascript"> function chkcontrol(j) { var total=0; for(var i=0; i < document.brandform.["ckb[]"].length; i++) { if(document.brandform.ckb[i].checked) { total =total +1; } if(total > 3) { alert("Please Select Maximum 3 Product") document.brandform.ckb[i].checked = false ; return false; } } } </script> HTML: <?php $sqllist="select subitem.id, subitem.subitem, subitem.shortdesc, subitem.image, subitem.bid, subitem.sid, subitem.model, subitem.sales, subitem.salesdesc, subitem.description, brand.brand ". "from subitem, brand ". "where subitem.bid = brand.primid and pid=$pid and itemid=$itemid order by id asc"; $pager = new PS_Pagination($conn, $sqllist, 10, 4, "pid=$pid&itemid=$itemid"); $rslist = $pager->paginate(); $k=0; while(@$relist=mysql_fetch_array($rslist)) { ?> <input type="checkbox" name="ckb" value="<?php echo $relist['id']; ?>" onclick="chkcontrol(this.value)" /> <?php } ?> PHP:
Try this out <script type="text/javascript"> function chkcontrol(j) { var total=0; for(var i=0; i < document.brandform.ckb.length; i++) { if(document.brandform.ckb[i].checked) { total =total +1; } if(total > 3) { alert("Please Select Maximum 3 Product") document.brandform.ckb[i].checked = false ; return false; } } } </script> Code (markup): You just did a simple mistake