Page 1 of 1

Multiple checkboxes forms and database

Posted: Fri Jun 27, 2003 7:17 pm
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

Posted: Sat Jun 28, 2003 7:11 am
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}']
???

Posted: Sat Jun 28, 2003 7:17 am
by twigletmac
Would it be possible to post an example of a set of checkboxes that would need to be looped through?

Mac

Posted: Sat Jun 28, 2003 7:59 am
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;
							}
	
						}

Posted: Sat Jun 28, 2003 8:10 am
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']}'";
							}

Posted: Sat Jun 28, 2003 5:53 pm
by mikusan
Has nobody ever built a form with checkboxes that could give me a hand? :oops:

Posted: Sat Jun 28, 2003 6:41 pm
by qartis

Code: Select all

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

Code: Select all

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

Posted: Sat Jun 28, 2003 6:50 pm
by patrikG
Also, this thread seems related.

Posted: Sat Jun 28, 2003 7:09 pm
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...