Thomas Lang
Thomas Lang

Reputation: 231

How can I create a checkbox form with array values

I have got an array with strings and I want to create a form with checkboxes, where each string has it´s own checkbox. The function $Service->read_command_cfg(); returns the correct array.

My PHP Code is as follows:

<form action="host_add.php" method = "post" >
<?PHP
include_once('service.php');
echo'Services:';
if (!empty($_POST['host_select']))
{
  $Service = new Service;
  $array_command_name_new = $Service->read_command_cfg();
}

foreach($array_command_name_new as $key=>$value)
{
  echo'<p><input type="checkbox" name="'.$value.'" value="'.$value.'/>' .$value. '</p>';    
}
?>
</form>

I got the following HTML-Output:

<form action="host_add.php" method = "post" >
Services:<p><input type="checkbox" name="command_name   notify-host" value="command_name notify-host/>command_name  notify-host</p><p><input type="checkbox" name="command_name notify-service" value="command_name notify-service/>command_name    notify-service</p>
</form>

On the Website I got only one checkbutton without text.

Upvotes: 1

Views: 76

Answers (1)

TBI
TBI

Reputation: 2809

There is syntax error -

foreach($array_command_name_new as $key=>$value)
{
    echo "<p><input type='checkbox' name='$value' value='$value' />$value</p>";
}

Upvotes: 1

Related Questions