1. Hey Guest Did you know you can win an Honor 10 phone worth ₹33,000 and an additional ₹70,000 in paytm vouchers, just by replying to some threads and taking part in the discussions happening in the Honor Hub?

    What are you waiting for? Start commenting and start winning! Remember to read the instructions posted here.

    Dismiss Notice

Checkbox group in javascript & PHP

Discussion in 'Programming' started by Sridhar_Rao, Jun 30, 2009.

Thread Status:
Not open for further replies.
  1. Sridhar_Rao

    Sridhar_Rao New Member

    Joined:
    Feb 16, 2007
    Messages:
    355
    Likes Received:
    1
    Trophy Points:
    0
    A series of checkbox elements are generated on the fly depending on the table contents and their number is unknown. The group of checkboxes are given the same name say name="chkb". I use the following code to toggle between select all and deselect all.
    Code:
    function check(cl){
    	if(document.getElementById('c1').checked == true){
    		for(i=0;i<cl.length;i++){
    			cl[i].checked = true;
    		}
    	}else{
    		for(i=0;i<cl.length;i++){
    			cl[i].checked = false;
    		}
    	}
    }
    When the user submits, I'd like to know how many were checked. The same code can be tweaked for this purpose.

    When the form is submitted only one checkbox value is available in $_POST array even if all are checked. This is probably because the checkbox group must be renamed differently such as name="chkb[]" and can be accessed later using
    foreach($_POST['chkb'] as $cd)

    If the checkboxes are named as chkb[], then the above javascript fails. How do I get around this problem?
     
  2. adatapost

    adatapost New Member

    Joined:
    Jul 5, 2009
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India/Gujarat/Mehsana
    Add Id attribute.
     
Thread Status:
Not open for further replies.

Share This Page