Search code examples
javascriptphpyiidom-events

Staff can only edit only few selected fields other fields will be readonly but admin can edit all the fields


Staff user can only edit some selected input fields, remaining input fields will be read-only, but Admin can edit all the fields. I am a beginner in PHP and i am trying to do it for the last few days but in-vain.

<div class="row">
    <div class="col-lg-6" style="margin-bottom: 20px;">
    <?php
    if(isset($_POST['submit'])) {
        if(($_POST['action']=='edit') || ($_POST['action2']=='edit')) { ?>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" class="form-horizontal" role="form" enctype="multipart/form-data">
            <?php 
            $edit_id=$_POST['selector'];
            $N = count($edit_id);
            for($i=0; $i<$N; $i++) {
                $result = mysql_query("SELECT * FROM ".TSURPHU." WHERE id='$edit_id[$i]'");
                while($row = mysql_fetch_array($result)) { ?>
                        <input type="hidden" name="id[]" id="id" value="<?php echo $row['id'] ?>">
                        <div class="form-group">
                            <div style="float:left">
                                <label><span class="tibetan">ཨང་à¼</span> Receipt No:</label>
                                <input type="text" class="form-control" style="width: 60% !important; color:#999999;" name="receipt_no[]" value="<?php echo $row['receipt_no']; ?>" readonly>
                            </div>
                            <div>
                                <label><span class="tibetan">* ཟླ་ཚེསà¼</span> Date: (yyyy-mm-dd)</label>
                                <input class="form-control" style="width: 20% !important;" type="text" readonly="readonly" name="subDate[]" value="<?php echo $row['subDate']; ?>" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label><span class="tibetan">* དད་དམ་མཆོག་à½à½´à¼‹à½¡à½„ས་པà¼</span> Received with thanks from:</label>
                            <input class="form-control" style="width: 40% !important" type="text" name="name[]" id="name" value="<?php echo $row['name'] ?>">
                        </div>
                        <div class="form-group">
                            <label><span class="tibetan">* à½à¼‹à½–ྱང་à¼</span> Address:</label>
                            <input class="form-control" style="width: 40% !important" type="text" name="address[]" id="address" value="<?php echo $row['address'] ?>">
                        </div>

Here is the form:

enter image description here

I want only (Receive from thanks) and (Address) can edit by staff user, but (Receipt) and (date) will be read-only, these fields can edited by admin only.


Solution

  • From your comment,

    Admin level = 5 where as staff level = 2..

    Assuming the fact that you have differentiated the admin and staff using $_SESSION i.e if an admin logs in, $_SESSION['user_level'] would be 5 and if a staff logs in, $_SESSION['user_level'] would be 2, your code inside while loop would be like this:

    // your code
    
    while($row = mysql_fetch_array($result)) { ?>
            <input type="hidden" name="id[]" id="id" value="<?php echo $row['id'] ?>">
            <div class="form-group">
                <div style="float:left">
                    <label> Receipt No:</label>
                    <input type="text" class="form-control" style="width: 60% !important; color:#999999;" name="receipt_no[]" value="<?php echo $row['receipt_no']; ?>"<?php if($_SESSION['user_level'] == 2){ echo ' readonly="readonly"'; } ?>>
                </div>
                <div>
                    <label> Date: (yyyy-mm-dd)</label>
                    <input class="form-control" style="width: 20% !important;" type="text" name="subDate[]" value="<?php echo $row['subDate']; ?>"<?php if($_SESSION['user_level'] == 2){ echo ' readonly="readonly"'; } ?>>
                </div>
            </div>
            <div class="form-group">
                <label> Received with thanks from:</label>
                <input class="form-control" style="width: 40% !important" type="text" name="name[]" id="name" value="<?php echo $row['name'] ?>">
            </div>
            <div class="form-group">
                <label> Address:</label>
                <input class="form-control" style="width: 40% !important" type="text" name="address[]" id="address" value="<?php echo $row['address'] ?>">
            </div>