Multiple checkboxes forms and database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Multiple checkboxes forms and database

Post by mikusan »

This is basically some help on how to deal with checkboxed and PHP. I have a form where there are multiple options... to delete and edit things mainly... so i have checkboxes and i need to see if they are checked... how do i do that in the fisrst place?

Say i have this:

Code: Select all

<input type="checkbox" name="check1" checked>
I figure the most plausible way would be to do:

Code: Select all

if (empty($_POST['check1'])) {
//Do stuff here
}
However, although i am not sure the above is right, the complication comes into the fact that the name field of each textbox is not static, and changes according to values taken from the database, mainly unique IDs.

I have successfully named all my chekcboxes with those ids, but now i am stuck and cannot think of a way to retrieve, or find out if those and which ones are checked...
To make it clearer here is some totally wrong code that i am hoping you can help me with.

Code: Select all

$SQL = mysql_query("SELECT * FROM am_users"); 

while($Data = mysql_fetch_assoc($SQL)) {

//Here i need to check if the checkbox that has one particular id in the database has been checked... 
if (empty($_POST['uidtakenfromdatabase'])){
//Do stuff
}
}
Do you see what i am trying to do? i can explain better if you have doubts...

Is there a better way to do what i am trying to do?

Thanks
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Okay not a lot of luck with this question, how about just helping me this much, i would like to know ho can i concatenate variables... i guess... i need to loop thriugh $_POST variables but check for defined field names...
would it be like:

Code: Select all

$_POST['{$variable}']
???
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Would it be possible to post an example of a set of checkboxes that would need to be looped through?

Mac
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Be my guest:

Code: Select all

$SQL = mysql_query("SELECT * FROM cd_users"); 
						//$nrows = mysql_num_rows($SQL);
	
						$flag = TRUE;
	
						while($Data = mysql_fetch_assoc($SQL)) {
	
							if ($flag) 
							{
								$main .=  '<tr> 
							    <td height="19" valign="top" bgcolor="#E4E4E4">' . $Data['uid']. '</td>
							    <td valign="top" bgcolor="#E4E4E4">' . $Data['username'] . '</td>
							    <td valign="top" bgcolor="#E4E4E4">' . $Data['email'] . '</td>
								<td valign="top" bgcolor="#E4E4E4"><input name="' .  $Data['uid'] . '" type="checkbox"></td>
							    </tr>';
								$flag = FALSE;
							}
				
							else
							{
								$main .=  '<tr> 
							    <td height="19" valign="top" bgcolor="#F7F7F7">' . $Data['uid']. '</td>
							    <td valign="top" bgcolor="#F7F7F7">' . $Data['username'] . '</td>
							    <td valign="top" bgcolor="#F7F7F7">' . $Data['email'] . '</td>
								<td valign="top" bgcolor="#F7F7F7"><input name="' .  $Data['uid'] . '" type="checkbox"></td>
							    </tr>';
								$flag = TRUE;
							}
	
						}
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

This variant of one of my attempts does not do anything...

Code: Select all

$SQL = mysql_query("SELECT * FROM cd_users"); 
	
						while($Data = mysql_fetch_assoc($SQL)) {
							if (!empty($_POST['{$Data['uid']}']))
							{
								mysql_query("DELETE FROM am_users WHERE uid='{$Data['uid']}'";
							}
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Has nobody ever built a form with checkboxes that could give me a hand? :oops:
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Code: Select all

if (!empty($_POST['{$Data['uid']}']))
Try

Code: Select all

if (!empty($_POST['$$Data['uid']']))
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Also, this thread seems related.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Thanks to both of ya guys but i figuredthings out on my own... apparently i had a misconception with checkboxes and i think they are the harder part of forms to work with PHP. First, the name field of each checkbox can be an array name such as profiles[], and to each checkbox you give a value, in my case $Data['uid'].

The way it works is that only the checked boxes are even passed to the $_POST superglobal... so here is the code in full working...and the result is pretty clean and professional ..I think:

The form:

Code: Select all

$SQL = mysql_query("SELECT * FROM cd_users"); 
						//$nrows = mysql_num_rows($SQL);
	
						$flag = TRUE;
	
						while($Data = mysql_fetch_assoc($SQL)) {
	
							if ($flag) 
							{
								$main .=  '<tr> 
							    <td height="19" valign="top" bgcolor="#E4E4E4">' . $Data['uid']. '</td>
							    <td valign="top" bgcolor="#E4E4E4">' . $Data['username'] . '</td>
							    <td valign="top" bgcolor="#E4E4E4">' . $Data['email'] . '</td>
								<td valign="top" bgcolor="#E4E4E4"><input name="profiles[]" value="' .  $Data['uid'] . '" type="checkbox"></td>
							    </tr>';
								$flag = FALSE;
							}
				
							else
							{
								$main .=  '<tr> 
							    <td height="19" valign="top" bgcolor="#F7F7F7">' . $Data['uid']. '</td>
							    <td valign="top" bgcolor="#F7F7F7">' . $Data['username'] . '</td>
							    <td valign="top" bgcolor="#F7F7F7">' . $Data['email'] . '</td>
								<td valign="top" bgcolor="#F7F7F7"><input name="profiles[]" value="' .  $Data['uid'] . '" type="checkbox"></td>
							    </tr>';
								$flag = TRUE;
							}
And this is the check...

Code: Select all

if ( !empty($_POST['profiles']) )
			{
				$profiles = $_POST['profiles'];

				foreach($profiles as $k => $v)
				{
					mysql_query("DELETE FROM cd_users WHERE uid='$v'");
				}

				$main .= '<table width="100%" height="16">
						  <tr>
							<td colspan="3" height="15"><B>User Deleted!</B></td>
					      </tr>
						  <tr>
							<td colspan="3" height="1" bgcolor="gray"></td>
						  </tr></table>';
			}
Hope this helps anyone with the same problem...
Post Reply