Hi,
I am unable to check the checkboxes with database values.
My table stores the data in below format. I want that when i load the form "useraccess.php" it check the access allowed ealier to the logged user and check the check boxes accordingly.

uid = 1
allowaccess = New User
url = newuser.php
-----------
uid = 1
allowaccess = New Product
url = New Product.php
-----------
uid = 2
allowaccess = New User
url = newuser.php

I tried this code but unable to get desired result.

1st method

<input type="checkbox" id="chk[]" name="chk[]" value="New User" <?php if ($access['allowaccess'] =='New User') echo 'checked="checked"'; ?>/>New User

---------------------

2nd method

<input type="checkbox" id="chk[]" name="chk[]" value="New Product" <?php if($access['allowaccess']=='New Product') {?> checked="checked" <?php }?>/>New Product

Recommended Answers

All 3 Replies

How are you pushing the data to the next page - via Posts or Gets? That's the key. You can call the same page in a form and either use POST or GET values (POST values are not visible in the URL) and then obtain their values in your code via $_POST['name'] or $_GET['name'] constructs.

If this is confusing, let me know and I can give some examples.

Thanks for the reply.
My full code is as under

<?php 
    session_start();
    $id=$_GET['uid']; 
    //Getting Data from users of the selected user
    include_once ('../conn.php');
    $sql="Select uname, ulevel, fullname, uid from users where uid = '".$id."'";
    $res=mysql_query($sql,$con);
    $row=mysql_fetch_array($res);

     //Getting Data from USERACCESS table
    $sql="Select * from useraccess where uid = '".$id."'";
    $res=mysql_query($sql,$con) or die('Error: ' . mysql_error());
    $access=mysql_fetch_array($res);

/*  while ($access=mysql_fetch_array($res))
    {
        echo "<h2>" . $access[1] . "</h2>"; 
    }
*/
    if($_POST['submit']) 
    { 
        include_once ('../conn.php');
        $sid=$_POST['tuid'];
        $chkd = $_POST['chk'];
        for ($i=0; $i<sizeof($chkd); $i++) 
        {
            $sql="insert into useraccess values('".$sid."', '".$chkd[$i]."', '".$chkd[$i]."')";
            $res=mysql_query($sql,$con) or die("Error " . mysql_error(). "<br>");

            echo $chkd[$i]."<br>";
        }

    }


?>

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Assign Roles</title>
        <link href="../styles.css" rel="stylesheet" type="text/css" />
        <link href="../CheckBoxStyle.css" rel="stylesheet" type="text/css">

        <style>
            fieldset {
                margin-top:10px;
                border-radius:10px;
                box-shadow:5px 5px 5px black;
                width:95%;
                background-color:white;
                padding-top:15px;
                padding-bottom:15px;
                }
            legend {
                background-color:#CCC;
                color:#006;
                text-transform:uppercase;
                border: 1px solid black;
                border-radius: 5px;
                padding-left:8px;
                padding-right:8px;
                font-size:small;
                }
        </style>
    </head>

    <body>
        <?php include('header.php'); ?>
    <!-- START content div -->
        <div style="width:900px; height:600px; border:1px solid navy; margin:0 auto; margin-top:10px;
            background-color:lightsteelblue;box-shadow:5px 5px 5px black; ">
        <!-- START Heading DIV -->
            <div style="width:200px; border-radius: 0px 0px 10px 10px; margin:0 auto;;
                             box-shadow:5px 5px 5px black; background-color:white; color:navy; ">
                <p style="text-align:center;font-size:20px">Assign Access</p> 
            </div>
        <!--END Heading DIV -->

        <div style="border:1px solid white; padding-left:15px; margin-top:10px; width:95%; 
                margin-left:8px; box-shadow:5px 10px 10px black;">
            <table style="width:95%;">
                <tr>
                    <td style="color:#009; font-weight:bold; width:70px">User Name:</td> <td><?php echo $row[0]; ?></td>
                    <td style="color:#009; font-weight:bold; width:70px">User Level:</td> <td><?php echo $row[1]; ?></td>
                    <td style="color:#009; font-weight:bold; width:70px">Full Name:</td> <td><?php echo $row[2]; ?></td>
                </tr>
            </table>

        </div>

        <div style="margin-top:20px; width:95%; padding-left:15px;">
            <form name="frmassignrole" action="assignrole.php" method="post">
                <!-- placing a hidden value for the selected user, will be used in saving data -->
                <input type="hidden" name="tuid" value="<?php echo $row[3]; ?>" />

                <fieldset>
                    <legend>User Management</legend>
                    <input type="checkbox" id="chk[]" name="chk[]" value="New User" 
                            <?php if ($access['allowaccess'] =='New User') echo 'checked="checked"'; ?>/>New User
                    <input type="checkbox" id="chk[]" name="chk[]" value="User Management" 
                            <?php if ($access['allowaccess'] =='User Management') echo 'checked="checked"'; ?>/>User Management

                </fieldset>

                <fieldset>
                    <legend>Accounts Management</legend>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Add Accounts" 
                            <?php if ($access['allowaccess'] =='Add Accounts') echo 'checked="checked"'; ?>/>Add Accounts
                    <input type="checkbox" id="chk[]" name="chk[]" value="Opening Balance" 
                            <?php if($access['allowaccess']=='Opening Balance') {?> checked="checked" <?php }?>/>Opening Balance
                    <input type="checkbox" id="chk[]" name="chk[]" value="View Ledger" 
                            <?php if($access['allowaccess']=='View Ledger') {?> checked="checked" <?php }?>/>View Ledger                    
                </fieldset>

                <fieldset>
                    <legend>Inventory Management</legend>

                </fieldset>

                <fieldset>
                    <legend>Voucher Management</legend>

                </fieldset>

                <fieldset>
                    <legend>Cheque Management</legend>

                </fieldset>

                <fieldset>
                    <legend>Reports</legend>

                </fieldset>

                <input type="submit" id="btnsubmit" name="submit" value="Submit" class="button" />
                <span><?php $show ?></span>

            </form>

        </div>
    <!-- END content div -->   

    </body>
</html>

DONE it by putting the database values to array, and search the arry while CHECK the checkbox. Check the below code with green color

<?php 
    session_start();
    $id=$_GET['uid']; 

    //Getting Data from table users of the selected user
    include_once ('../conn.php');
    $sql="Select uname, ulevel, fullname, uid from users where uid = '".$id."'";
    $res=mysql_query($sql,$con);
    $row=mysql_fetch_array($res);

     //Getting Data from USERACCESS table
    $sql="Select * from useraccess where uid = '".$id."'";
    $qry=mysql_query($sql,$con) or die('Error: ' . mysql_error());
    $count=mysql_num_rows($qry);
    if ($count >=1) 
    {
        while ($a = mysql_fetch_array($qry)) 
        {
            $ary=$a[1];
            $myvals[]=$ary;
        }
    } 


/*  while ($access=mysql_fetch_array($qry))
    {
        echo "<h6>" . $access[1] . "</h6>"; 
    }
*/
    if($_POST['submit']) 
    { 
        include_once ('../conn.php');
        $sid=$_POST['tuid'];
        $chkd = $_POST['chk'];
    //Deleteing the old data for the selected user
        if ($count >= 1) 
        {
            $sql="Delete from useraccess where uid = '".$sid."'";
            $res=mysql_query($sql,$con) or die("Error :" . mysql_error());
        }   

        for ($i=0; $i<sizeof($chkd); $i++) 
        {
            $sql="insert into useraccess values('".$sid."', '".$chkd[$i]."', '".$chkd[$i]."')";
            $res=mysql_query($sql,$con) or die("Error " . mysql_error());

//          echo $chkd[$i]."<br>";       /Show saved data
        }

    }


?>

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Assign Roles</title>
        <link href="../styles.css" rel="stylesheet" type="text/css" />
        <link href="../CheckBoxStyle.css" rel="stylesheet" type="text/css">

        <style>
            fieldset {
                margin-top:10px;
                border-radius:10px;
                box-shadow:5px 5px 5px black;
                width:190px;
                height:190px;
                background-color:white;
                padding-top:15px;
                padding-bottom:15px;
                line-height:25px;
                text-align:left;
                }
            legend {
                background-color:#CCC;
                color:#006;
                text-transform:uppercase;
                border: 1px solid black;
                border-radius: 5px;
                padding-left:4px;
                padding-right:4px;
                font-size:small;
                /*text-shadow:2px 2px black;*/
                }
        </style>

        <script language="JavaScript">
            function selectAll()            //Check all Checkboxes
            {
                checkboxes = document.getElementsByName('chk[]');
                //alert(checkboxes.length);
                /*for(var i in checkboxes)
                    checkboxes[i].checked = 'checked';*/ //taken from net
                    for(var i=0; i <=checkboxes.length; i++)
                        checkboxes[i].checked = 'checked';
            }

            function unselectAll()          //Uncheck all checkboxes
            {
                checkboxes = document.getElementsByName('chk[]');
                    for(var i=0; i <=checkboxes.length; i++)
                        checkboxes[i].checked = '';
            }

        </script>
    </head>

    <body>
        <?php include('header.php'); ?>
    <!-- START content div -->
        <div style="width:900px; height:620px; margin:0 auto; margin-top:10px;
            background-color:lightsteelblue;box-shadow:5px 5px 5px black; border-radius:10px; ">
        <!-- START Heading DIV -->
            <div style="width:200px; border-radius: 0px 0px 10px 10px; margin:0 auto;;
                             box-shadow:5px 5px 5px black; background-color:white; color:navy; ">
                <p style="text-align:center;font-size:20px">Assign Access</p> 
            </div>
        <!--END Heading DIV -->

        <div style="border:1px solid white; padding-left:15px; margin-top:10px; width:95%; 
                margin-left:8px; box-shadow:5px 10px 10px black;">
            <table style="width:95%;">
                <tr>
                    <td style="color:#009; font-weight:bold; width:70px">User Name:</td> <td><?php echo $row[0]; ?></td>
                    <td style="color:#009; font-weight:bold; width:70px">User Level:</td> <td><?php echo $row[1]; ?></td>
                    <td style="color:#009; font-weight:bold; width:70px">Full Name:</td> <td><?php echo $row[2]; ?></td>
                </tr>
            </table> 
        </div>

        <div style="margin-top:20px; width:95%; padding-left:125px; text-align:center;  border:2px solid red;">
            <form name="frmassignrole" action="assignrole.php" method="post">
                <!-- placing a hidden value for the selected user, will be used in saving data -->
                <input type="hidden" name="tuid" value="<?php echo $row[3]; ?>" />

                <fieldset style="float:left;">
                    <legend>User Management</legend>
                    <input type="checkbox" id="chk[]" name="chk[]" value="New User"
                        <?php if(!empty($myvals))if(in_array("New User",$myvals)) 
                            {echo 'checked="checked"';} ?> />New User <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="User Management"
                         <?php if(!empty($myvals))if(in_array("User Management",$myvals)) 
                            {echo 'checked="checked"';} ?> />User Management
                </fieldset>

                <fieldset style="float:left;">
                    <legend>Accounts Management</legend>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Add Accounts" 
                        <?php if(!empty($myvals))if(in_array("Add Accounts",$myvals)) 
                            echo 'checked="checked"' ?> />Add Accounts <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Opening Balance"
                        <?php if(!empty($myvals))if(in_array("Opening Balance",$myvals)) 
                            echo 'checked="checked"' ?> />Opening Balance <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="View Ledger" 
                        <?php if(!empty($myvals))if(in_array("View Ledger",$myvals)) 
                            echo 'checked="checked"' ?> />View Ledger
                </fieldset>

                <fieldset style="float:left;">
                    <legend>Inventory Management</legend>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Unit of Measure" 
                        <?php if(!empty($myvals))if(in_array("Unit of Measure",$myvals)) 
                            echo 'checked="checked"' ?>/>Unit of Measure <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Compound UOM" 
                        <?php if(!empty($myvals))if(in_array("Compound UOM",$myvals)) 
                            echo 'checked="checked"' ?>/>Compound UOM <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Products" 
                        <?php if(!empty($myvals))if(in_array("Products",$myvals)) 
                            echo 'checked="checked"' ?> />Products <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Purchase"
                        <?php if(!empty($myvals))if(in_array("Purchase",$myvals)) 
                            echo 'checked="checked"' ?> />Purchase <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Modify Purchase" 
                        <?php if(!empty($myvals))if(in_array("Modify Purchase",$myvals)) 
                            echo 'checked="checked"' ?>/>Modify Purchase <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Sales"
                        <?php if(!empty($myvals))if(in_array("Sales",$myvals)) 
                            echo 'checked="checked"' ?> />Sales <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Modify Sale" 
                        <?php if(!empty($myvals))if(in_array("Modify Sale",$myvals)) 
                            echo 'checked="checked"' ?> />Modify Sale
                </fieldset>

                <fieldset style="float:left;">
                    <legend>Voucher Management</legend>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Cash Receipt"
                        <?php if(!empty($myvals))if(in_array("Cash Receipt",$myvals)) 
                            echo 'checked="checked"' ?> />Cash Receipt <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Cash Payment" 
                        <?php if(!empty($myvals))if(in_array("Cash Payment",$myvals)) 
                            echo 'checked="checked"' ?>/>Cash Payment <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Bank Receipt" 
                        <?php if(!empty($myvals))if(in_array("Bank Receipt",$myvals)) 
                            echo 'checked="checked"' ?>/>Bank Receipt <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Bank Payment" 
                        <?php if(!empty($myvals))if(in_array("Bank Payment",$myvals)) 
                            echo 'checked="checked"' ?>/>Bank Payment <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="JVs"
                        <?php if(!empty($myvals))if(in_array("JVs",$myvals)) 
                            echo 'checked="checked"' ?> />JVs <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Modify JVs"
                        <?php if(!empty($myvals))if(in_array("Modify JVs",$myvals)) 
                            echo 'checked="checked"' ?> />Modify JVs 
                </fieldset>

                <fieldset style="float:left;">
                    <legend>Cheque Management</legend>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Cheque Receipt" 
                        <?php if(!empty($myvals))if(in_array("Cheque Receipt",$myvals)) 
                            echo 'checked="checked"' ?> />Cheque Receipt <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Cheque Payment" 
                        <?php if(!empty($myvals))if(in_array("Cheque Payment",$myvals)) 
                            echo 'checked="checked"' ?>/>Cheque Payment <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Cheque Return To"
                        <?php if(!empty($myvals))if(in_array("Cheque Return To",$myvals)) 
                            echo 'checked="checked"' ?> />Cheque Return To <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Cheque Return From" 
                        <?php if(!empty($myvals))if(in_array("Cheque Return From",$myvals)) 
                            echo 'checked="checked"' ?>/>Cheque Return From <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Cheque Clearance" 
                        <?php if(!empty($myvals))if(in_array("Cheque Clearance",$myvals)) 
                            echo 'checked="checked"' ?>/>Cheque Clearance <br>
                    <input type="checkbox" id="chk[]" name="chk[]" value="Modify Clearance" 
                        <?php if(!empty($myvals))if(in_array("Modify Clearance",$myvals)) 
                            echo 'checked="checked"' ?>/>Modify Clearance 
                </fieldset>

                <fieldset style="float:left">
                    <legend>Reports</legend>
                </fieldset>

                <div style=" margin-top:10px;  float:left; width:70%;">
                    <input type="submit" id="btnsubmit" name="submit" value="Submit" class="button" />
                    <input type="button" id="btncheckall" name "btncheckall" value="Check All" class="button"
                         onClick="selectAll()" />
                    <input type="button" id="btnuncheckall" name "btnuncheckall" value="Uncheck All" class="button"
                         onClick="unselectAll()" />
                </div>

            </form>

        </div>
    <!-- END content div -->   

    </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.