Dynamic Checkboxes

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Methonis
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2003 12:57 am

Dynamic Checkboxes

Post by Methonis »

Ok, I'm just about at the end of my wits with this one. I'm a php nub and simply cannot get this despite trying so many things. I have an options page. On that page, I'm querying the database to pull all the membergroups and put them on the page with a checkbox next to them. Up to this point, I'm fine. Now, what I need to do, is that if a checkbox is checked, it's going to pass that value into the membergroup table under the column cantitle. So, for example if I checked the box by administrator, in my table is would say:
membergroup | cantitle

Administrator | 1

And if they're not checked, they'd say 0 in the cantitle column...I just can't figure this out. The closest I got was actually getting it to put the value into there, but if you then unchecked it it wouldn't pass the 0 value, it would just skip the loop and not change the 1 value...I'd appreciate some help, I'm really lost. Here's what I've done:

Code: Select all

$request = mysql_query("SELECT * FROM `{$db_prefix}membergroups` WHERE ID_GROUP=1 OR ID_GROUP=8;") or database_error(__FILE__, __LINE__);
	while ($row = mysql_fetch_array($request))
	
	{
		echo '
							<tr>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><font size="2">' . $row['membergroup'] . '</font></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><input type="checkbox" name="chk~[' . $row['ID_GROUP'] . ']" value=". $row['cantitle'] . '/></td>
							</tr>';
	}
	
	$request = mysql_query("SELECT * FROM `{$db_prefix}membergroups` WHERE `grouptype`=0 AND ID_GROUP>2 AND ID_GROUP!=8;") or database_error(__FILE__, __LINE__);
	while ($row = mysql_fetch_array($request))
	{
		echo '
							<tr>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><font size="2">' . $row['membergroup'] . '</font></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><input type="checkbox" name="chk~[' . $row['ID_GROUP'] . ']" value="'. $row['cantitle'] . '/></td>
							</tr>';
	}
	
	echo '
							<tr>
								<td class="titlebg" bgcolor="' . $color['titlebg'] . '" colspan="3">
									<a href="javascript:reqWin(''' . $scripturl . '?action=helpadmin;help=addctitle'')" class="help"><img src="' . $imagesdir . '/helptopics.gif" border="0" alt="' . $txt[119] . '" /></a>
									<font size="2" class="text1" color="' . $color['titletext'] . '"><b>' . $txt['memtitle2'] . '</b></font>
								</td>
							</tr>';
							
	$request = mysql_query("SELECT * FROM `{$db_prefix}membergroups` WHERE `grouptype`=1;") or database_error(__FILE__, __LINE__);
	while ($row = mysql_fetch_array($request))
	{
		echo '
							<tr>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><font size="2">' . $row['membergroup'] . '</font></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><input type="checkbox" name="chk~[' . $row['ID_GROUP'] . ']" value="'. $row['cantitle'] . '/></td>
							</tr>';
	}
That's calling up all the different membergroups, which works fine. And then this is in my post php:

Code: Select all

reset($_POST); 
while(list($key, $val) = each($_POST)) { 
  if(substr($key, 0, 3) == "chk") { 
    $arKey = explode("~", $key); 
    $id = $arKey;
   		$request = mysql_query("UPDATE {$db_prefix}membergroups SET cantitle='$val' WHERE ID_GROUP='$id';") or database_error(__FILE__, __LINE__);   
  }
}
I just don't get it. I'm really hoping someone can explain to me how to actually accomplish this.

Sincerely,
Methonis
Flood
Forum Newbie
Posts: 11
Joined: Wed Jun 25, 2003 4:52 pm

Post by Flood »

Hi!

I am not sure what I am going to write works but it is just an idea....

You know the total number of rows you have displayed... Why not simply check isSet($_POST["chk~[{$i}]"]) for all of them? ==> loop over $i; if isSet returns true ==> set 1; otherwise set 0...

Hope it helps.

/Flood
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

When you leave a checkbox unchecked, the browser doesn't include the variable when submitting a form, therefore the only way to check if a checkbox has been left unchecked is to see whther it has been submitted or not.
Methonis
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2003 12:57 am

Post by Methonis »

So what's the easiest way to check if it's submitted? And would that check go in my posting page or go into the display page?

Thanks again
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

I think the only easy way to do it is for each checkbox to have a corresponding hidden field, i.e.:

You HTML will look like this (where 12 is the group id):

Code: Select all

<input type="hidden" name="chk[12][hidden]" value="1" />
<input type="checkbox" name="chk[12][checked]" value="1" />
Then in your php you can:

Code: Select all

foreach ($_POST['chk'] as $group_id => $details)
{
    if ($details['checked']) echo $group_id.' was checked!.<br />'."\n";
}
I think this should work. If not do a var_dump($_POST['chk']) to find out eactly how it's being submitted.
Methonis
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2003 12:57 am

Post by Methonis »

Ok, changed my display to this:

Code: Select all

$request = mysql_query("SELECT * FROM `{$db_prefix}membergroups` WHERE ID_GROUP=1 OR ID_GROUP=8;") or database_error(__FILE__, __LINE__);
	while ($row = mysql_fetch_array($request))
	
	{
		echo '
							<tr>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><font size="2">' . $row['membergroup'] . '</font></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><input type="checkbox" name="' . $row['ID_GROUP'] . '" value="1"'. $row['cantitle'] . '/></td>
							</tr>';
	}
	
	$request = mysql_query("SELECT * FROM `{$db_prefix}membergroups` WHERE `grouptype`=0 AND ID_GROUP>2 AND ID_GROUP!=8;") or database_error(__FILE__, __LINE__);
	while ($row = mysql_fetch_array($request))
	{
		echo '
							<tr>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><font size="2">' . $row['membergroup'] . '</font></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><input type="checkbox" name="[' . $row['ID_GROUP'] . '" value="1"'. $row['cantitle'] . '/></td>
							</tr>';
	}
	
	echo '
							<tr>
								<td class="titlebg" bgcolor="' . $color['titlebg'] . '" colspan="3">
									<a href="javascript:reqWin(''' . $scripturl . '?action=helpadmin;help=addctitle'')" class="help"><img src="' . $imagesdir . '/helptopics.gif" border="0" alt="' . $txt[119] . '" /></a>
									<font size="2" class="text1" color="' . $color['titletext'] . '"><b>' . $txt['memtitle2'] . '</b></font>
								</td>
							</tr>';
							
	$request = mysql_query("SELECT * FROM `{$db_prefix}membergroups` WHERE `grouptype`=1;") or database_error(__FILE__, __LINE__);
	while ($row = mysql_fetch_array($request))
	{
		echo '
							<tr>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><font size="2">' . $row['membergroup'] . '</font></td>
								<td class="windowbg2" bgcolor="' . $color['windowbg2'] . '"><input type="checkbox" name="' . $row['ID_GROUP'] . '" value="1"'. $row['cantitle'] . '/></td>
							</tr>';
	}
Basically just taking the check[] out, and then I tried this for the post:

Code: Select all

$request = mysql_query("SELECT ID_GROUP FROM `{$db_prefix}membergroups`;") or database_error(__FILE__, __LINE__); 
    while ($row = mysql_fetch_array($request)) 
    { 
        $id_group = $row['ID_GROUP'];
		if (isset($HTTP_POST_VARS[$row['ID_GROUP']]))
		{
			 $request2 = mysql_query("UPDATE {$db_prefix}membergroups SET cantitle='1' WHERE ID_GROUP='$id_group';") or database_error(__FILE__, __LINE__);
			 	else
					$request2 = mysql_query("UPDATE {$db_prefix}membergroups SET cantitle='0' WHERE ID_GROUP='$id_group';") or database_error(__FILE__, __LINE__);				 
		}   
    }
When I submit I just get a blank screen. View source just has the <HTML><HEAD></HEAD><BODY></BODY></HTML> tags. Do I have something wrong with syntax or something?
Post Reply