HTML / PHP problem; checkboxes

Miscellaneous Forums/General Discussion/HTML / PHP problem; checkboxes

Okay, I have a really weird script here, but it says in the comments that it does what it does for a very good reason so I'd best not touch it.

For my web framework, I have variables sorted into files; one file contains just default settings for all variables used for page creation, and the other is advanced information for some of those variables which will be automatically added to the site administration page.
It's all pretty cool, technically (ignoring my ignorance of the wonders of databases) :)

Unfortunately, I have encountered a problem.
For the sake of user friendliness, boolean values are done with check boxes in the admin page. It turns out that an HTML form return, however, does not bother to return a check box when it is not checked.
Since the admin page's saving method loops through returned form values instead of variables (weird reasons to do with cleanliness and "cleverness"), this means that an unchecked checkbox is not observed as setting a variable to 0!
I've thought of a few solutions involving hidden fields, but they all involve a complete reliance on Javascript which I am trying to avoid.

I'm a bit lost :(

the source would be nice thing to see :)

I don't know if this will help but a few years ago I did a form which involved checkboxes and the idea was to email back to the customer the whole HTML form as it appeared on screen with the information filled in, i.e. checkboxes ticked and unticked.

Here's the relevant snippets. Probably worthless as you mention values instead of variables :)

HTML Snippet
<input type="checkbox" name="a1" value="checked">
<font face="Verdana" size="2">
contact me to arrange a <b>Mortgage &amp; Debt and General Insurance Review</b>


PHP Snippet (from the mailer script)
<input type="checkbox" name="a1" value="checked" '.$a1.'>
<font face="Verdana" size="2">
contact me to arrange a <b>Mortgage &amp; Debt and General Insurance Review</b>


why not doing it that way:
<?php

// initialize variable-defaults
if(!isset($a1)) $a1 = 0;


// do updatestuff if form is submitted
if(count($_POST)) {
    $sql = "UPDATE/INSERT INTO ... SET a1='$a1' WHERE blabla...";
}

// load variables
else {
     $sql = "SELECT a1 FROM blabla...";
}


// print form
echo '<input type="checkbox" name="a1" value="1"  '.($a1 ? 'checked' : '').'>';
?>


or whats your problem?

Go with ASP.NET, bloody make life a whole lot easier when using VB! :)

Dabz

Thanks for the help...
I think I'll just go with having two radio buttons instead of a single check box. I'll call it user friendliness :)

Of course, I could rearrange the code that parses submitted content so that it parses variables and compares them to the form submission, but it doesn't seem as nice somehow.

I would post the source, but it probably would scare people more than help anyone.
(Such a mess...)

...But, just in case, here are the two chunks involved here.

The global variable 'page' is an object for the page being edited, which contains all variables. It has an overloaded Get and Set function so that any variables not set for that page are instead retrieved from the default variables list.
$def_varsListdata contains extra variable information, generally to tell the administration page how to represent a visual selector for that variable.

Checks a list of variables and draws them as their extra settings suggest:
function ListVarsEditors($vars,$descWidth='180px') //Hard-coded to this horrible mess of a script. Don't even try to use elsewhere!

	{
		foreach( array_keys($vars) as $var )
		{
			echo('<tr><td style="width:'.$descWidth.';">'.$vars[$var]['descrip'].':</td><td>');
			
			$name='pdata_'.$var;
			$value=$GLOBALS['page']->{$var};
			$varmode='text';
			if( isset($vars[$var]['varmode']) ){ $varmode=$vars[$var]['varmode']; }
			
			
			switch( $varmode )
			{
			case 'text':
				echo('<input type="text" name="'.$name.'"style="width:100%;" value="'.htmlspecialchars($value,ENT_COMPAT).'">');
				
				break;
			case 'boolean':
				echo('<input type="checkbox" name="'.$name.'" value="1" ');
				if( $value==1 ){ echo('checked'); }
				echo('>');
				break;
				
			case 'option':
				echo('<select name="'.$name.'" style="width:100%">');
				foreach( $vars[$var]['option'] as $choice )
				{
					echo('<option value="'.$choice.'" ');
					if( $value==$choice ){ echo('selected'); }
					echo('>'.$choice.'</option>');
				}
				echo('</select>');
				break;
			}
			
			echo('</td></tr>');
		}
	}


Code to parse form return and write out the new variables (yep, it's ugly):
		foreach( array_keys($_POST) as $item)
		{
			if( substr($item,0,6)=='pdata_' )
			{
				$value=stripslashes($_POST[$item])
				fwrite($sectdata,substr($item,6).'='.$value.'&');
			}
		}