选中Zend表格复选框

Ale*_*tau 2 php checkbox zend-framework zend-form

如何根据变量值在Zend Framework中选中复选框?例如,我有$some_value = 'yes';- 复选框应检查,否则取消选中.谢谢.

Jak*_*e N 11

以为我会对此有所了解,你的问题写得不好,但我会尽我所能.下面有几个解决方案,我还没有测试过.试一试.

例1

 // create your checkbox
 $checkbox_element = new Zend_Form_Element_Checkbox("mycheckbox");     


 // Set the value to 1 if needed
 if($some_value == "yes")
 {
      $checkbox_element->setValue(1);
 }
Run Code Online (Sandbox Code Playgroud)

例2

 // Check if value is set
 if($some_value == "yes")
 {
      // Create checkbox element and Set the attribute 'checked' to 'checked' 
      $checkbox_element = new Zend_Form_Element_Checkbox("mycheckbox", array("checked" => "checked"); 
 }   
 else
 {
      // Othrewise create the checkbox which is not checked
      $checkbox_element = new Zend_Form_Element_Checkbox("mycheckbox"); 
 }
Run Code Online (Sandbox Code Playgroud)