[thelist] dump question, but...

John Corry lists at neoncowboy.com
Fri Jun 10 11:49:00 CDT 2005


 
> Its been awhile and I'm getting rusty.  I'm doing some 
> redirecting back to a php form page, its a pretty long form 
> so I dont want people to have to fill in all the stuff if 
> they missed one thing or even 30 things.  Anyway, I have a 
> checkbox with about 9 items they can check, and I want that 
> remembered, so I something like this.
> 
> <input type="checkbox" name="housecond[]" value="1" <? if
> ($housecond=="1") print "checked" ?>>
> 
> Yes I have done this before, but its friday and I have been 
> racing through this site for a friend.

Race faster with functions! (mind the wrap)

// string checkbox_field ([string name [, string value [, string label [,
string match]]]])

// This function returns an HTML checkbox field. The optional third argument
// will be included immediately after the checkbox field, and the pair
// is included inside a HTML <nobr> tag - meaning that they will be
// displayed together on the same line.  If the value of the
// second or third argument matches that of the fourth argument,
// the checkbox will be 'checked' (i.e., flipped on).

function checkbox_field ($name="", $value="", $label="", $match="")
{
	$checked = ($value == $match || $label == $match) ? "checked" : "";
	$output = <<<EOQ
<nobr><input type="checkbox" name="$name" value="$value" $checked>
$label</nobr>
EOQ;
	return $output;
}

So, to call this function:

<?= checkbox_field('housecond', $_POST['housecond'], '',
$_POST['housecond']?>

I need to rewrite my form element functions as a class for consistency with
my other HTML class stuff...but am sort of on the verge of using PEAR for
all this stuff.

When you get done racing and have a little time, do yourself a favor and
check out the HTML, form and table classes at PEAR. There are some very cool
tools for doing this kind of repetitive, arduous work over there.
http://pear.php.net

Good luck,
John Corry




More information about the thelist mailing list