How to get selected index of checkbox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zabivb
    New Member
    • Sep 2008
    • 17

    How to get selected index of checkbox?

    Hai all,

    I managed to add dynamic checkboxes(ie., if i add data in database checkbox will automatically generated in another form

    Coding Create table trole(id,role,i sadmin)
    =======
    <?php

    $sql="select id,role from trole where isadmin='0'";
    $result=mysql_q uery($sql);
    $count=mysql_nu m_rows($result) ;
    $role='';
    $ID='';
    $role1='';
    $i = 0;
    if ($count < 1) {
    echo "There are No Records";
    }
    else
    {
    while ($count > $i) {
    $ID = mysql_result($r esult,$i,'id');
    $role = mysql_result($r esult,$i,'role' );
    //$NA= mysql_result($p Result,$i,"Your Name");
    //print out checkboxes and escape quotes
    $_SESSION['ID']=$ID;
    $_SESSION['role']=$role;
    echo "<input type=\"checkbox \" name=\"role[]\" value=\"$ID\">" ;
    echo($role);
    echo($ID);
    $i++;
    }
    }

    Example outputs
    ===============
    2(id)student(ro le), 3(id)faculty(ro le), 4(id)others

    Now my problem is if i select id 2 and 4 it should add 2&4 in database but i',m getting default value 4 for every data (ex: if i select 2&3 it should add 2&3 but i'm getting as 4 for every one.)

    Please help, Apologise if my english is not good.
    Thanks
  • Sudaraka
    New Member
    • Jan 2011
    • 55

    #2
    You should be receiving the checkbox values as an array in the backend.
    Code:
    $_GET['role'][0], $_GET['role'][1], etc.
    or
    $_POST['role'][0], $_POST['role'][1], etc.
    If you post the code where you capture the post-back data and insert to DB, might be able to help more.

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      In your PHP Code to handle the $_POST['role'] array, are you looping through each array item?

      Comment

      • Zabivb
        New Member
        • Sep 2008
        • 17

        #4
        ya i'm using while loop

        coding:
        $i = 0;
        if ($count < 1) {
        echo "There are No Records";
        }
        else
        {
        while ($count > $i) {
        $ID = mysql_result($r esult,$i,'id');
        $role = mysql_result($r esult,$i,'role' );
        //$NA= mysql_result($p Result,$i,"Your Name");
        //print out checkboxes and escape quotes
        $_SESSION['ID']=$ID;
        $_SESSION['role']=$role;
        echo "<input type=\"checkbox \" name=\"role[]\" value=\"$ID\">" ;
        echo($role);
        echo($ID);
        $i++;
        }
        }
        =======
        it will generate dynamic checkbox with values, now if i check any items it should take that items alone and i'm unable to do that, kindly help me out... Thanks in advance

        Comment

        • Sudaraka
          New Member
          • Jan 2011
          • 55

          #5
          When you name an HTML form element with ending [] and it's post back, PHP will recognize it as an array. So you have to do something like:
          Code:
          if(is_array($_POST['role']))
          {
          	foreach($_POST['role'] as $roleId)
          	{
          		//Do something with $roleId here
          	}
          }
          (Please note that $_POST could be a $_GET depending on your HTML form settings)

          My feeling is that the code you have posted is where you display the form, and this loop myself and Samishii23 talk about should happen in the code where you handle the form post back.

          Comment

          Working...