how to limit validate max 3 check box which are fetch from database (dynamic)

Discussion in 'PHP' started by dineshsingh1984, Jan 12, 2011.

  1. #1
    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:
     
    Last edited: Jan 12, 2011
    dineshsingh1984, Jan 12, 2011 IP
  2. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #2
    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
     
    Tanya Roberts, Jan 12, 2011 IP
  3. dineshsingh1984

    dineshsingh1984 Active Member

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    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&amp;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:
     
    dineshsingh1984, Jan 12, 2011 IP
  4. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #4
    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
     
    Tanya Roberts, Jan 12, 2011 IP
  5. dineshsingh1984

    dineshsingh1984 Active Member

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    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&amp;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:
     
    dineshsingh1984, Jan 13, 2011 IP
  6. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #6
    Try this out :D

    
    <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
     
    Tanya Roberts, Jan 13, 2011 IP