my db has members, and statuses. each member can have 1 current status.
I have a code that decides what status to give what member. thing is, it can give 2 possible statuses, and then the site admin should approve one of them(if at all)

I made a report page with a table, each row has a member-status that is waiting approval/decline.
I added a check box near each row so that the admin can check multiple statuses and approve/deny them at one click (added a add/deny all checked button)
but now I have a problem, what if the admin checks 2 boxes for the same member (when there are more than 1 possible status).. this shouldn't be possible, so then I thought, make a radio button instead, but if I group all radio buttons (as I did with the checkboxes, I pass them as an array when the approve all button is clicked) then I can chose only 1 status no matter how many members are in the table..if I group the radio buttons for each member, how do I pass all the radio buttons that were checked using POST?
I can't think on any other solution..
there's also the problem that I don't really know how many statuses each member has, so I can't group them as I print them. I just use the select *from tbl query and it prints the someone's status 1 by 1 so how can I know who's will be the next status printed? I can only check after it's printed if it's the same as the one before..
hmm...need help ASAP please! sort of last minute mistake I found, too stressed to even think straight!

Dani AI

Generated

Short summary and a practical pattern to avoid duplicate choices per member while keeping the ability to “clear” a selection.

As described, the UI must allow at most one approved status per member. was right to suggest separate groups — a clearer and easier-to-process pattern is to render each member’s options with PHP-style array names (for example name="status[123]"). That produces an associative $_POST['status'] keyed by member id, so server-side code can iterate per member directly and validate each choice.

Render (server-side) by grouping rows by member before output. Example pattern:

<?php
// build $byMember from DB rows (ORDER BY member_id helps)
$byMember = [];
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) $byMember[$r['member_id']][] = $r;

foreach ($byMember as $memberId => $statuses) {
  echo "<tr><td>Member {$memberId}</td></tr>";
  foreach ($statuses as $s) {
    echo '<tr><td><input type="radio" name="status[' . $memberId . ']" value="' . $s['id'] . '"></td>';
    echo '<td>' . htmlspecialchars($s['label']) . '</td></tr>';
  }
}
?>

To handle the “can’t uncheck a radio” problem: either include an explicit “No decision” option per group or add a tiny JS toggle that lets a second click clear the checked radio. Plain-JS toggle (works without libraries):

document.addEventListener('mousedown', function(e){
  if (e.target.tagName === 'INPUT' && e.target.type === 'radio') e.target.wasChecked = e.target.checked;
});
document.addEventListener('click', function(e){
  if (e.target.tagName === 'INPUT' && e.target.type === 'radio' && e.target.wasChecked) e.target.checked = false;
});

Always enforce the rule server-side as well. If using status[member_id], iterate $_POST['status'], verify each status id belongs to that member and is still pending, then update. If JavaScript is disabled or malicious input arrives, the server-side check prevents processing two statuses for the same member. Accessibility note: give each group a legend/label so screen readers can identify the member’s choices.

Recommended Answers

All 3 Replies

Something like this should work, where the name has the format status_"id_user": status_1, status_2 ... status_23.

<input type="radio" name="status_1" value="to be" /> to be
<input type="radio" name="status_1" value="not to be" /> not to be

<input type="radio" name="status_2" value="to be" /> to be
<input type="radio" name="status_2" value="not to be" /> not to be

<input type="radio" name="status_3" value="to be" /> to be
<input type="radio" name="status_3" value="not to be" /> not to be

This will give you:
$_POST
$_POST
$_POST

Then use array_keys to extract user ids, you will end up with something similar to this:

<?php
$a = array_keys($_POST);
$array = array();
foreach($a as $key)
{
	if(preg_match('/status/i',$key))
	{
		$b = explode('_',$key);
		echo 'id: ' . $b[1] . ' value: ' . $_POST[$key];
		
		# uncomment below to build an array
		# $array[] = array($b[1] => $_POST[$key]);
	}
}

#print_r($array);
?>

hope it's useful, bye :)

this is great, thank you so much! but now I see that if I click one of the radio buttons I can't unclick it..so if the report has 40 statuses and 30 were checked and then u click one more by mistake u can't undo it(in a case where the admin doesn't want to approve nor deny a status at that time)..
so I think maybe adding a javascript check if 2 statuses were checked for the same member and show error popout so the admin can uncheck on box and continue..

Yes you can, in dojo I use:

function unsetRadio() {
    dojo.query('#group1 input[type=radio]:checked').attr('checked',false);
}

it should be simpler with getElementById, bye.

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.