Jump to content

Controlling checkboxes to manage user access to a CMS menu


fohanlon

Recommended Posts

Hi

I need some help.  Heres the problem.  I have built a manage users page in a CMS and for each user there are 10 checkboxes (each one when ticked means a user can have access to perform that option).  Many users are displayed.

Problem occurs when I try to update /change the checkboxes.  What is happening is that regardless of the random order I click checkboxes when the form is submitted to update the mysql database the table fields are updated starting at the first checkbox onwards

Heres what I mean:

Lets take one user and lets assume that of the checkboxes 1-10 say 2,3,8,9 are checked.  Then I check 4 and submit the form.

Ideally I would like the fields the table that represent , 2,3,4,8,9 to be set to Yes but what is happening is that fields 1,2,3,4,5 in the table are being set to Yes.

Heres the code I use for the form:

<form name="update" action="manage_users.php" method="POST">
                        <div align="left">
                          <table border="0" cellspacing="0" cellpadding="0" width="100%" align="left">                           
                            <tr>
                              <td valign="middle"><strong>Full Name </strong></td>
                              <td><b>Username</b></td>
                              <td><b>Password</b></td>
                              <td><b>Level</b></td>
                              <td><div align="center">
                                  <table width="330"  border="0" align="center" cellpadding="0" cellspacing="0">
                                    <tr>
                                      <td width="33"><div align="center"><img src="images/icons/edit_pages.gif" alt="Edit Pages" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/upload.gif" alt="Upload PDF Documents" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/cms_help.gif" alt="View CMS Help" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/stat.gif" alt="View Visitor Statistics" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/seng.gif" alt="View Search Engine Statistics" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/password.gif" alt="Change Password" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/users.gif" alt="Manage Users" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/new_property.gif" alt="Add New Property" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/modify_properties.gif" alt="Modify Existing Properties" width="24" height="24"></div></td>
                                      <td width="33"><div align="center"><img src="images/icons/diaries.gif" alt="View Diary" width="24" height="24"></div></td>
                                    </tr>
                                  </table>
                              </div>
  </td>
                              <td>
  <div align="center">
  <?php echo "<b>Del</b>"; ?>
  </div>
  </td>
                            </tr>
<?php
for($count = 0; $userdetails = mysql_fetch_array($sql4); $count++)
{
?>
                            <tr>
                              <input type="hidden" name="list_<?php echo $count; ?>[]" value="<?php echo $userdetails[0]; ?>">
                              <td width="112" valign="middle"><div align="left">
                                <input name="list_<?php echo $count; ?>[]" type="text" class="formitems" value="<?php echo stripslashes($userdetails["Full_Name"]); ?>" size="16">
</div></td>
                              <td width="91" valign="middle"><input name="list_<?php echo $count; ?>[]" type="text" class="formitems" value="<?php echo $userdetails["Username"]; ?>" size="12">
                              </td>
                              <td width="91" valign="middle"><div align="left">
                                  <input name="list_<?php echo $count; ?>[]" type="text" class="formitems" value="<?php echo $userdetails["Password"]; ?>" size="12">
                              </div></td>
                              <td width="99" valign="middle">
  <select name="list_<?php echo $count; ?>[]" class="formitems" id="level">
                                  <?php if($userdetails["Level"] == "Administrator") { ?>
                                  <option value="Administrator" selected>Administrator</option>
  <option value="User">User</option>
                                  <?php } else { ?>
                                  <option value="User" selected>User</option>
                                  <option value="Administrator">Administrator</option>
                                  <?php } ?>
                              </select></td>
                              <td width="344" valign="middle">
  <div align="center">
                                <table width="330"  border="0" align="center" cellpadding="0" cellspacing="0">
                                  <tr>
                                    <td width="33"><div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["Edit_Pages"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div></td>
                                    <td width="33"><div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["PDF_Upload"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div></td>
                                    <td width="33"><div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["CMS_Help"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div></td>
                                    <td width="33"><div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["Visitor_Stats"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div></td>
                                    <td width="33"><div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["Seng_Stats"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div></td>
                                    <td width="33"><div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["Change_PW"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div></td>
                                    <td width="33">
<div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["Manage_Users"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div>
</td>
                                    <td width="33">
<div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["Add_Property"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div>
</td>
                                    <td width="33">
<div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["Manage_Properties"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div>
</td>
                                    <td width="33">
<div align="center">
                                        <input name="list_<?php echo $count; ?>[]" type="checkbox" value="Yes" <?php if($userdetails["View_Diaries"] == "Yes") echo ' checked'; else echo ' unchecked'; ?>>
                                    </div>
</td>
                                  </tr>
                                </table>
                              </div></td>
                              <td width="33" valign="top" bgcolor="#FF3300">
                                <div align="center">
<?php
echo "<input type=\"checkbox\" name=\"deleteuser[]\" value=\"" . $userdetails["RecordID"] . "," . stripslashes($userdetails["Full_Name"]) . "," . $userdetails["Level"]  . "\" />\n";
?>
</div></td>
                            </tr>
                            <?php } ?>
                            <tr>
                              <td colspan="3">
      <input type="hidden" name="task" value="">
                                  <input type="hidden" name="num_rows" id="num_rows" value="<?php echo $count; ?>">
                                  <br><br>
                                  <input name="update" type="button" class="formbuttons" value="Update List" onClick="update_users()">
                              </td>
  <td colspan="3">
  <div align="right">
  <br><br>
  <input type="button" name="delete" class="formbuttons" value="Delete Selected" onclick="viewAll()"/>&nbsp;&nbsp;&nbsp;
  </div>
  </td>
                            </tr>
                          </table>
                        </div>
                    </form>

When the page is submitted to itself:


$date = date("Y-m-d");
$task = $_POST['task'];
if($task == "update")
{
for($i=0; $i < $_POST['num_rows']; $i++)
{
$rec = "list_".$i;
list($rid, $full_name, $un, $pw, $level, $edit_pages, $pdf_upload, $cms_help, $visitor_stats, $seng_stats, $change_pw, $manage_users, $add_property, $manage_properties, $view_diaries) = $_POST[$rec];
$full_name = addslashes($full_name);
$updatesql = mysql_query("UPDATE users SET Full_Name = '$full_name', Username = '$un', Password = '$pw', Level = '$level', Edit_Pages = '$edit_pages', PDF_Upload = '$pdf_upload', CMS_Help = '$cms_help', Visitor_Stats = '$visitor_stats', Seng_Stats = '$seng_stats', Change_PW = '$change_pw', Manage_Users = '$manage_users', Add_Property = '$add_property', Manage_Properties = '$manage_properties', View_Diaries = '$view_diaries', Date = '$date' WHERE RecordID = '$rid'");
if(!$updatesql) { die("Couldn't execute update of users " . $full_name . ": " . mysql_error()); }
}
}

if ($task == "remove_users")
{
if (count($_POST['deleteuser']) >= 1)
{
  for ($i=0; $i < count($_POST['deleteuser']); $i++)
  {
$ids = explode(",", $_POST['deleteuser'][$i]);
$id = $ids[0];
$deleteuser = mysql_query("DELETE FROM users WHERE RecordID = '$id'");
if (!$deleteuser) { die('Problem deleting user: ' . mysql_error()); }
  }
}
$message = 1;
}

$sql4 = mysql_query("SELECT * FROM users ORDER BY Full_Name ASC") or die("Couldn't execute users query");
?>

Javascript that is used when the button on the form is subnmitted:

<script type="text/javascript">
<!-- This is to hide JavaScript from non java enabled browsers
function viewAll()
{
var LB = "\n";
var msg= "";

  for(i=0; i < update["deleteuser[]"].length; i++)
  {
    if(update["deleteuser[]"][i].checked == true)
{
var temp = update["deleteuser[]"][i].value;
var temp2 = temp.split(",");
msg += temp2[1] + ", " + temp2[2] + LB;
}
  }

  if(msg == "")
  {
  alert("You have not selected any users to delete.");
  }
  else
  {
if (confirm("Do you really want to delete these users(s)? (OK = Yes  Cancel = No)" + LB + LB + msg))
{
document.update.task.value = "remove_users";
document.update.submit();
}
  }
}

function update_users()
{
document.update.task.value="update";
document.update.submit();
}
// End of Script Hiding -->
</script>

Many thanks for the help

Fergal.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.