Jump to content

update series of radio buttons


rondog

Recommended Posts

I have a table that shows all the users in my table. Each user is either a user or an admin. I have radio buttons for each user determining if they are a user or an admin

radiobuttons.jpg

 

Right now I have each RB group named userlevel0, userlevel1, userlevel2 etc..

 

How can I get all the groups dynamically? I attempted an html array but failed.

Link to comment
Share on other sites

POST them dynamically..I don't know how many rows their are unless I do a select to get the num rows first which I am trying to avoid.

 

If I get the num rows first and its equal to, say 5, then I know their are 5 groups of radio buttons meaning:

 

userlevel0,

userlevel1,

userlevel2,

userlevel3,

userlevel4

 

Link to comment
Share on other sites

I cant be any more clear dude. Each row is its own group in the same form. When I submit I want to POST all the radio button groups, but since I don't know how many rows their are I can just do $_POST['userlevel0'], $_POST['userlevel1']..I need to know how many rows their is. I think I am just going to do the select before I post solution...

Link to comment
Share on other sites

group = radio button group...

 

If you look at the picture in the OP, you can see the rows.. each row you can select admin or user...each row is its own radio button group..when I POST, I don't know how many rows their are.

 

This is my code so far:

 

if (isset($_POST['savebtn']))
{
    // post the radio button groups
}

Link to comment
Share on other sites

I tried that..the problem is, each user is their own group. if I do userlevel[], all the rows are one big group.

 

I don't know what this means.  Arrays are the way to go here but it seems that you have done something wrong before and are unwilling to follow the advice and try it again.

 

Post your form code.

Link to comment
Share on other sites

Let me rephrase that quote. I tried using an HTML array. Each user (row) has its own radio button group. If I set the name of the radio button group to userlevel[], its one giant row meaning I can only select 1 radio button across all 4 rows you see in the image above. This is my form code:

<form method="post">
<table cellpadding="4" border="0">
	<tr style="text-align:center;font-weight:bold;background-color: #777777; color: #ffffff;">
		<td>Username</td>
		<td>Email</td>
		<td>First Name</td>
		<td>Last Name</td>
		<td>Deactivate?</td>
		<td>User Level</td>
	</tr>
	<tr class="usertable">
		<td><div align="center">some username</div></td>
		<td><div align="center">someemail@domain.com</div></td>
		<td><div align="center">some first name</div></td>
		<td><div align="center">some last name</div></td>
		<td><div align="center"><a href="?do=mngu&a=deactivate&id=28">Deactivate</a></div></td>
		<td><label>
			<input type="radio" name="userlevel0" value="1" checked="checked"/>
			Admin</label>
			<br/>
			<label>
			<input type="radio" name="userlevel0" value="0" />
			User</label></td>
	</tr>
	<tr style="background-color: #4f4f4f" class="usertable">
		<td><div align="center">some username</div></td>
		<td><div align="center">someemail@domain.com</div></td>
		<td><div align="center">some first name</div></td>
		<td><div align="center">some last name</div></td>
		<td><div align="center"><a href="?do=mngu&a=deactivate&id=27">Deactivate</a></div></td>
		<td><label>
			<input type="radio" name="userlevel1" value="1" checked="checked"/>
			Admin</label>
			<br/>
			<label>
			<input type="radio" name="userlevel1" value="0" />
			User</label></td>
	</tr>
	<tr class="usertable">
		<td><div align="center">some username</div></td>
		<td><div align="center">someemail@domain.com</div></td>
		<td><div align="center">some first name</div></td>
		<td><div align="center">some last name</div></td>
		<td><div align="center"><a href="?do=mngu&a=deactivate&id=26">Deactivate</a></div></td>
		<td><label>
			<input type="radio" name="userlevel2" value="1" />
			Admin</label>
			<br/>
			<label>
			<input type="radio" name="userlevel2" value="0" checked="checked"/>
			User</label></td>
	</tr>
	<tr style="background-color: #4f4f4f" class="usertable">
		<td><div align="center">some username</div></td>
		<td><div align="center">someemail@domain.com</div></td>
		<td><div align="center">some first name</div></td>
		<td><div align="center">some last name</div></td>
		<td><div align="center"><a href="?do=mngu&a=deactivate&id=25">Deactivate</a></div></td>
		<td><label>
			<input type="radio" name="userlevel3" value="1" checked="checked"/>
			Admin</label>
			<br/>
			<label>
			<input type="radio" name="userlevel3" value="0" />
			User</label></td>
	</tr>
</table>
<input type="submit" name="savebtn" value="Save" />
</form>

 

Now you can see all the radio button groups...I need to know how many radio button groups to post when I hit save and since the users table constantly changes in size, I need to know how many to post

Link to comment
Share on other sites

Wow, that's was like trying to wring water out of a rock  ;)  But just based on the code that you've shown, I would use the id as the array index:

 

<td><div align="center"><a href="?do=mngu&a=deactivate&id=28">Deactivate</a></div></td>   
<td><label>
<input type="radio" name="userlevel[28]" value="1" checked="checked"/>
Admin</label>
<br/>
<label>
<input type="radio" name="userlevel[28]" value="0" />
User</label></td>

 

Then on the receiving page, if all you want is the count you would do:

 

$count = count($_POST['userlevel']);

 

But depending on what you're doing you would normally just foreach():

 

foreach($_POST['userlevel'] as $id => $value) {
   // UPDATE table_name SET user_level = $value WHERE user_id = $id
}

 

Or something similar.

 

 

Link to comment
Share on other sites

yeah sorry, I'll use the term radio button group rather than group next time to be more clear. Also, I wasn't aware you could define the HTML array index. That is very good to know. With your suggesting, this is what I ended up with and it works. Thanks for stickin through

if (isset($_POST['savebtn']))
{
foreach ($_POST['userlevel'] as $id => $value)
{
	$query = mysql_query("UPDATE members SET userlevel = '" . $_POST['userlevel'][$id] . "' WHERE id = '" . $id . "'");
}
}

Link to comment
Share on other sites

Glad to hear it, and you can replace $_POST['userlevel'][$id] with $value (less code).

 

Also, you should really use mysql_real_escape_string() on any variables in your query that are coming from the form.

 

I tried using $value and it seems to be empty. If i echo $value its just blank

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.