0

i'm working on permissions in php,my requirement is i have list of menus..based on user role i have to give access to them.so a single user have access to multiple menus..my idea is i will select check boxes and store it in single database columns.and if i check any check box the value should be 'y' in database column field.and if not check means value should be 'x' in database column field(Example:y,x,x,y) i have a table like this following.. i'm working on permissions in php,my requirement is i have list of menus..based on user role i have to give access to them.so a single user have access to multiple menus..my idea is i will select check boxes and store it in single database columns.and if i check any check box the value should be 'y' in database column field.and if not check means value should be 'x' in database column field(Example:y,x,x,y) i have a table like this following..

role id,role name,permissions,description...

my code:

<form class="form-horizontal tasi-form" method="POST"  name="register-form" id="register-form">


                        <div class="form-group">
                            <label class="col-sm-2 col-sm-2 control-label">Roles Title</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control round-input" name="roletitle" id="roletitle">

                            </div>

                        </div>
                           <div class="form-group">
                            <label class="col-sm-2 col-sm-2 control-label">Permission</label>
                            <div class="col-sm-10">
                               <input type="checkbox" name="sb[]" id="checkbox_php" value="EMPLOYEES"/><label for="checkbox_php">EMPLOYEES</label> 
                                <br/>
                                <input type="checkbox" name="sb[]" id="checkbox_asp" value="UNIT"/><label for="checkbox_asp">UNIT</label><br/>  
                                <input type="checkbox" name="sb[]" id="checkbox_asp" value="SERVICES"/><label for="checkbox_asp">SERVICES</label><br/>  
                                <input type="checkbox" name="sb[]" id="checkbox_asp" value="Appointment"/><label for="checkbox_asp">Appointment</label><br/>    

                            </div>

                        </div>

                        <div class="form-group">
                            <label class="col-lg-2 col-sm-2 control-label">Roles Description</label>
                            <div class="col-lg-10">
                     <textarea name="description" id="editor1" class="form-control"  cols="30" rows="10"></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-2 col-sm-2 control-label"></label>
                            <div class="col-lg-10">
                <input type="hidden" value="roles" name="requesttype"/>
                       <button type="submit" class="btn btn-success">Save</button>
                       </div>
                       </div>
                    </form>
php code:
<?php
include("../config.php"); 
if(isset($_POST['requesttype'])&&$_POST['requesttype'] == 'roles'){
   $insertInfo=array();
    $insertInfo['ROLES_TITLE'] = $_REQUEST['roletitle'];
    $insertInfo['PERMISSIONS'] = $_REQUEST['permission'];
    $insertInfo['ROLES_DESCRIPTION'] = $_REQUEST['description'];
    $date=date('Y-m-d H:i:s');
    $insertInfo['CREATED_ON']=$date;
    $insertinfo['UPDATED_BY']='';
    $insertInfo['UPDATED_ON']=$date;
$res=$db->insert('ROLES', $insertInfo);
if($result)
{?>
//header("location:city");
<script>
window.location.href='rolesa';
</script>
<?php }
} ?>
1
  • 1
    Simply use implode(',',$_POST['sb']); Commented Aug 13, 2015 at 7:23

2 Answers 2

0

I will suggest a alternative way, If you can store real value in db.

Like this,

dbCheckbox = "";
if(isset($_POST['sb'])){
  $dbCheckbox = implode(',',$_POST['sb']);
}

When you retrieve data you can,

$dbCheckboxExplode = array();
if(!empty($checkDBValueOnThatCol)){
  $dbCheckboxExplode = explode(",", checkDBValueOnThatCol);
}

And You better to keep checkbox value on display page as array like this,

<?php
$checkboxAllValues = array('EMPLOYEES', 'UNIT');

foreach($checkboxAllValues  as $value){
?>
<input type="checkbox" name="sb[]" id="checkbox_asp" value="<?php echo $value; ?>"/><label for="checkbox_asp"><?php echo $value; ?></label><br/> 
<?php
}
?>

When db data retrieve page,

<?php
$checkboxAllValues = array('EMPLOYEES', 'UNIT');

foreach($checkboxAllValues  as $value){
    $checked = "";
  if(in_array($value, $dbCheckboxExplode)){
      $checked = ' checked="checked"';
  }
?>
<input <?php echo  $checked; ?> type="checkbox" name="sb[]" id="checkbox_asp" value="<?php echo $value; ?>"/><label for="checkbox_asp"><?php echo $value; ?></label><br/> 
<?php
}
?>

You can do this way:

<?php
    $allValues = array('EMPLOYEES', 'UNIT');
    $dbCheckbox = "";
    if(isset($_POST['sb'])){
       foreach($_POST['sb'] as $value){
           if(in_array($value, $allValues)){
             $dbCheckbox .=  'Y';
           }else{
             $dbCheckbox .=  'X';
           }
            $dbCheckbox .=  ',';
       }
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

hi...i'm able to store,but if checked any of checkbox in database it should be 'y' else it should be 'n' example in datABASE FIELD: Y,X,X,Y MEANS eMPLOYEE CHECKED,UNIT NOT CHECKED,SERVICES NOT CHECKED,APPOINTMENT CHECKED
hi this is not working..how to display only checked check box values in $allValues;
Solved by changing your code little bit..any way thank u
0

Create the table and with column which has datatype as varchar. Give values to each check boxes and add the checked checkboxes values in that column

<input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
<input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>

Add comma separated values in database column check the complete answer here

PHP : insert multiple check boxes values into one MySQL cloumn

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.