Help to get my self teach exercise to work

I am new to PHP and I have been using a teach yourself book and am completely stumped on one of the exersises. To keep this brief, I have copied the code as supplied in the exersise verbatum but I cannot get my program to work. The object of the exersise is to display a page where the User can tick boxes to indicate their lieisure activities. The page should then be redisplayed to allow the User to make changes. My program displays the page okay but when the Submit button is clicked to submit my selection the previously ticked boxes are redisplayed blank. Here is the code:-

function generate_checkboxes($name, $options, $default=array())
{
if (!is_array($default))
$default = array();

	foreach($options as $value => $label) {
	  $html .= "<INPUT TYPE=CHECKBOX ";
	  if (in_array($value, $default))
             	    $html .= "CHECKED ";
                          $html .= "NAME=\"{$name}[]\" VALUE=\"$value\">";
	 $html .= $label . "<br>";
	}
	return($html);
}	

$options = array(“movies” => “Going to the movies”,
“music” => “Listening to music”,
“sport” => “Playing or watching sports”,
“travel” => “Traveling”);

$html = generate_checkboxes(“interests”, $options, $interests);

?>

Please Select your interests

<?php print $html;?>

If it’s any help we use PHP version 5.4 here.

I hope somebody can help if only to restore me to sanity
Many Thanks
Keith

Of course this can be modified even more to where you can accept multiple choices and other things, but I think that something you can discover on you own. (HINT: Maybe another array?)

[php]<?php
function generate_checkboxes($default = ‘sports’, $options)
{
$html = ‘’;
foreach($options as $key => $value) {

    $html .= '<input class="checkBox" type="checkbox" ';
    if ($default == $key) $html .= 'checked ';
    $html .= 'name="' . $key . '" value="'. $value . '">';
  	$html .= '<label class="labelStyle">' . $key . '</label>';
  }
  return $html;

}

$options = array(
“movies” => “Going to the movies”,
“music” => “Listening to music”,
“sports” => “Playing or watching sports”,
“travel” => “Traveling”
);

?>
<!doctype html>

Document .labelStyle { display: block; clear: right; width: 100px; height: 40px; } .checkBox { float: left; display: inline-block; }

Please Select your interests

<?php echo (!isset($html)) ? generate_checkboxes('music', $options) : NULL;?> [/php]

Thanks for your help Strider64 it is very much apprecaited, I’ll look at what you have sent and try and work it out.

Sponsor our Newsletter | Privacy Policy | Terms of Service