Jump to content

[SOLVED] Checkbox issues


NerdConcepts

Recommended Posts

Ok this is what it is: Setting Permissions for custom users

Displays a checkbox for each of the 9 permissions for each "Custom" user and has a form button to update it at the bottom.

 

This is what it does: Currently there are 2 custom users:

When I check a permission on the first row and update it, it works fine

When I check a permission for both users it works

But, when I select a permission for the second user, but the first user has the box not checked and I submit it, the second user doesn't get the right value, that value moves to the first user....not sure exactly why....

 

I have gone through this problem once before, but for the life of me I cannot remember how I fixed it nor can I find the code I wrote to fix it.

 

and, finally here is the code...

 

if (isset($_POST['subSet'])) {
$ctr = -1;
foreach ($_POST['user_id'] as $x) {
	$ctr++;
	$perm1 = (isset($_POST['perm_inven_assign'][$ctr])) ? '1' : '0';
	$perm2 = (isset($_POST['perm_inven_import'][$ctr])) ? '1' : '0';
	$perm3 = (isset($_POST['perm_inven_pricing'][$ctr])) ? '1' : '0';
	$perm4 = (isset($_POST['perm_jobs_manage'][$ctr])) ? '1' : '0';
	$perm5 = (isset($_POST['perm_jobs_import'][$ctr])) ? '1' : '0';
	$perm6 = (isset($_POST['perm_labor_manage'][$ctr])) ? '1' : '0';
	$perm7 = (isset($_POST['perm_tickets_manage'][$ctr])) ? '1' : '0';
	$perm8 = (isset($_POST['perm_tc_manage'][$ctr])) ? '1' : '0';
	$perm9 = (isset($_POST['perm_parts_manage'][$ctr])) ? '1' : '0';

	$query = "UPDATE users SET
	perm_inven_assign='$perm1',
	perm_inven_import='$perm2',
	perm_inven_pricing='$perm3',
	perm_jobs_manage='$perm4',
	perm_jobs_import='$perm5',
	perm_labor_manage='$perm6',
	perm_tickets_manage='$perm7',
	perm_tc_manage='$perm8',
	perm_parts_manage='$perm9'
	WHERE user_id='$x'";
	$result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
}
}
?>
<center>
<table border="0" cellpadding="0" cellspacing="0" align="center" width="100%">
<tr>
	<td>
		<form action="permissions.php" method="post">
		<fieldset>
			<legend>Permissions Management</legend>

			<table border="0" cellspacing="0" cellpadding="0" width="100%">
				<tr valign="top">
					<td width="150">User ID/Name</td>
					<td align="center">Assign Inventory</td>
					<td align="center">Import Inventory</td>
					<td align="center">Inventory Pricing</td>
					<td align="center">Manage Jobs</td>
					<td align="center">Import Jobs</td>
					<td align="center">Manage Labor</td>
					<td align="center">Manage Ticket Reports</td>
					<td align="center">Manage TCs</td>
					<td align="center">Manage Parts/Materials</td>
				</tr>
				<?PHP
				$query = "SELECT * FROM users WHERE user_type='Custom' ORDER BY user_id";
				$result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());

				$bg = '#f4f3f2'; // Set background colour.

				while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$bg = ($bg=='#f4f3f2' ? '#f0efed' : '#f4f3f2'); // Switch background colour.
					?>
					<tr bgcolor="<?PHP echo $bg; ?>">
						<td>
							<?PHP echo $row['user_id'] . ' - ' . $row['user_name']; ?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_inven_assign'] == 1) {
								?><input type="checkbox" name="perm_inven_assign[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_inven_assign[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_inven_import'] == 1) {
								?><input type="checkbox" name="perm_inven_import[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_inven_import[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_inven_pricing'] == 1) {
								?><input type="checkbox" name="perm_inven_pricing[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_inven_pricing[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_jobs_manage'] == 1) {
								?><input type="checkbox" name="perm_jobs_manage[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_jobs_manage[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_jobs_import'] == 1) {
								?><input type="checkbox" name="perm_jobs_import[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_jobs_import[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_labor_manage'] == 1) {
								?><input type="checkbox" name="perm_labor_manage[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_labor_manage[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_tickets_manage'] == 1) {
								?><input type="checkbox" name="perm_tickets_manage[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_tickets_manage[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_tc_manage'] == 1) {
								?><input type="checkbox" name="perm_tc_manage[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_tc_manage[]" /><?PHP
							}
							?>
						</td>
						<td align="center">
							<?PHP
							if ($row['perm_parts_manage'] == 1) {
								?><input type="checkbox" name="perm_parts_manage[]" checked="checked" /><?PHP
							} else {
								?><input type="checkbox" name="perm_parts_manage[]" /><?PHP
							}
							?>
						</td>									
					</tr>
					<input type="hidden" name="user_id[]" value="<?PHP echo $row['user_id']; ?>" />
					<?PHP
				}
				?>
			</table>
			<br />
			<div align="right"><input type="submit" value="Set Permissions" name="submit" class="regButton" /></div>
			<input type="hidden" name="subSet" value="TRUE" />
		</fieldset>
		</form>
	</td>
</tr>
</table>

Link to comment
Share on other sites

What is happening is that, no matter which user has th check box pressed, the array "perm_[something]_[something]" will have the value at the top. When you loop through each user, if the second one has the box checked, it goes into the first one because it is still in the same array. For example, if user 1 has the option checked, it puts it into the array. That works fine, as well as if user 2 checks also, but if user one doesn't check and user two does, user two's check goes into the array position that you are checking to see if user one is checked.

 

The way you have the code written, there isn't really a way to fix it because the value will always be pushed to the top of the array.

Link to comment
Share on other sites

TheFilmGod: I get that your basically doing what I'm doing, but I'm not sure how your code is working. So the answer doesn't make since to me.

 

MadTechie: Not exactly sure what you mean by that one.

 

Only other (kind of crappy) option that I know, that I know of, is to make it where you have to select one user at a time to modify their permissions.

Link to comment
Share on other sites

One thing you could do (with javascript) is make a hidden input who's value is activated when one of the permissions for that user is checked. Then you would have to check them all when you uncheck one, but it would work.

 

I think MadTechie's idea is the simplest. He is saying, basically, that you should have different variables for each user. The code he posted makes each permissions into a two dimensional array that contains the user id in the first dimension and the form results in the next.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.