DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   how to active and inactive an account using check box??? (http://www.daniweb.com/forums/thread162327.html)

freeonlinedatin Dec 15th, 2008 4:03 am
how to active and inactive an account using check box???
 
This is my code can you expllain how the user may activate or inactivate with check box....
<?php 
mysql_connect("localhost","root","rootwdp");
mysql_select_db("rams");
if(isset($_REQUEST['submit']))

{
        $arr=$_REQUEST['chk'];
        foreach($arr as $key => $value)
        {
               
                $sql=mysql_query("update ram set status=1 where emp_id=$value");
        }
               
                  $sql1="select * from ram where status=1";
                  $res1=mysql_query($sql1);
                 
?> 
  <form action="" method="post" name="form">
        <table border="1"><th>Username</th><th>Empid</th><th>Designation</th><th>Check box</th>
        <?php   
                          while($row1=mysql_fetch_array($res1))
                  {
       
                                ?>       
              <tr> 
                        <td><?php echo $row1[0];?></td>
                 
                 
                <td align="center"><?php echo $row1[1];?></td>
               
       
                          <td align="center"> <?php echo $row1[2];?> </td>
                        <td align="center">
        <input type="hidden" name="chk" id="chk" value="<?php $row1[1];?>"/>
        <input type="checkbox" id="chk" name="chk"  value="<?php $row1[1];?>"  />
        </td>
                         
                         
                  </tr>
               
                  <?php }
                  } ?>
                </table></form>
               
<?php
mysql_connect("localhost","root","rootwdp");
mysql_select_db("rams") or die("unable to select");
 $query = "SELECT * FROM `ram`";
 $dd=mysql_query($query);
       
  ?>

 
<form action="" method="post" name="form">
          <table border="1"><th>Username</th><th>Empid</th><th>Designation</th><th>Check box</th>
        <?php   
                          while( $row=mysql_fetch_array($dd))
                                {
                                ?>       
              <tr> 
                        <td><?php echo $row[0];?></td>
                 
                 
                <td align="center"><?php echo $row[1];?></td>
               
       
                          <td align="center"> <?php echo $row[2];?> </td>
        <td><input type="hidden" name="chk" id="chk" value="<?php $row[1];?>"/>
            <input type="checkbox" id="chk" name="chk"  value="<?php $row[1];?>"/></td>
                         
                         
                  </tr>
               
                  <?php } ?>
                </table>
                <table><tr><td> <input type="submit" name="submit" value="Submit" /></td></tr></table>
                </form>

sikka_varun Dec 16th, 2008 12:30 pm
Re: how to active and inactive an account using check box???
 
Hi...
Why are you giving two controls the same name..
One is checkbox and other hidden field...

Just put one control...
Use checkbox and give it name as chk[] ... Yes you read right.. Its an array.. Secondly.. give its value as user's id or name (basically the field from which you will search the user in the database..

<?php 
mysql_connect("localhost","root","rootwdp");
mysql_select_db("rams");

if($_REQUEST['submit'] == "Activate")
{
        $arr=$_REQUEST['chk'];
       
        $empids = implode(",",$arr);

        // This will transform your array into a string with empids separated by comma e.g. 1,2,5,7

        mysql_query("update ram set status=1 where emp_id in $empids");
        //this will set all employee ids in one go..
}

if($_REQUEST['submit'] == "Deactivate")
{
        $arr=$_REQUEST['chk'];
       
        $empids = implode(",",$arr);

        // This will transform your array into a string with empids separated by comma e.g. 1,2,5,7

        mysql_query("update ram set status=0 where emp_id in $empids");
        //this will set all employee ids in one go..
}




        $query = "SELECT * FROM `ram`";
        $dd=mysql_query($query);
       
        ?>

 
        <form action="" method="post" name="form">
          <table border="1"><th>Username</th><th>Empid</th><th>Designation</th><th>Check box</th>
        <?php   
                          while( $row=mysql_fetch_array($dd))
                                {
                                ?>       
              <tr> 
                        <td><?php echo $row[0];?></td>
                 
                 
                <td align="center"><?php echo $row[1];?></td>
               
       
                          <td align="center"> <?php echo $row[2];?> </td>
        <td><input type="checkbox" id="chk[]" name="chk[]"  value="<?php $row[1];?>"/></td>
                         
                         
                  </tr>
               
                  <?php } ?>
                </table>

                <table>
                        <tr>
                                <td> <input type="submit" name="submit" value="Activate" /></td>
                                <td> <input type="submit" name="submit" value="Deactivate" />
                </tr></table>

                </form>


All times are GMT -4. The time now is 2:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC