Hello,
I'm (still) having a problem with the initialization of 'checked' checkboxes.
Basically I want to generate a form with amongst others some checkboxes. When the checkboxes are on, one or more subcheckboxes (and other input types) will follow. Initially I want to have some check boxes on and some of. I'm using functions and objects to be able to reuse the code and be more flexible. Any help is appreciated. Thanks in advance.
Note: I think, though am not sure, that the initial 'hard coded' checked causes the problem. When all checkboxes are initially off, everything worked fine (though maybe not with the version below).
Here is a stripped version of my code. I must be doing something wrong.
//**********main program
<head>
<title>Checkbox inpput test...</title>
<script language=javascript>
<!-- hide from older browsers
function publish() {
document.myform.submit();
}
// unhide -->
</script>
</head>
<body>
<?php
include ("myobject.class");
session_start();
session_register ("serial_myobject");
if (!isset($serial_myobject))
{
$amyobject = new myobject();
}
else
{
$amyobject=unserialize($serial_myobject);
}
$amyobject->set_characteristics();
?>
<form name="myform" enctype="multipart/form-data" method="post" action=<?php echo basename($PHP_SELF); ?>>
<table align="left">
<?php
include "mycharacteristics.php";
?>
</table>
</form>
<?php
$serial_myobject=serialize($amyobject);
?>
</body>
//***class definition
<?php
class basicobject
{
function basicobject()
{
return;
}
function get_option($option)
{
return $this->$option;
}
function set_option($option, $new_value)
{
$this->$option=$new_value;
return;
}
}
// mainobject
class myobject extends basicobject
{
var $varone;
var $varoneinputtype;
var $vartwo;
var $vartwoinputtype;
function myobject()
{
$this->varone=1;
$this->varoneinputtype="checkbox";
$this->vartwo=0;
$this->vartwoinputtype="checkbox";
return;
}
function get_characteristics(&$chararray)
{
$chararray["varone"]=$this->varone;
$chararray["vartwo"]=$this->vartwo;
return;
}
function set_characteristics()
{
$this->varone = 1;
$this->vartwo = 0;
return;
}
}
?>
//****mycharateristics.php
<?php
include "myfunctions.php";
$amyobject->get_characteristics($characteristicsarray);
reset($characteristicsarray);
while ( list($optionname,$optionindex) = each( $characteristicsarray ) )
{
$optiontype=$amyobject->get_option($optionname."inputtype");
echo "<tr>";
display_characteristic ($optiontype, $amyobject, $optionname, $line);
echo $line;
echo "$optionname<br></tr>";
}
?>
//**********myfunctions.php
<?php
function checkbox ($name, $value, $name_val) {
$result ="<input type=checkbox ";
$result.="name=\"$name\" ";
$result.="value=\"$value\" ";
$result.="onClick=publish(this.form) ";
$checked=($name_val == 1 ? checked : '');
if ($checked) {$result.=" checked";}
$result.=">";
return $result;
}
function display_characteristic ($optiontype, &$myobject, &$optionname, &$line)
{
if ($$optionname)
{
$myobject->set_option($optionname,$$optionname);
}
else
{
$optionvalue=$myobject->get_option($optionname);
$myobject->set_option($optionname,$optionvalue);
}
$optionvalue=$myobject->get_option($optionname);
$value="1";
$line= $optiontype ($optionname, $value, $optionvalue);
return;
}
?>