Php – Zend_form_element_select onchange in zend framework

PHPzend-formzend-framework

I have a form named createDevice.php as:

class Admin_Form_CreateDevice extends Zend_Form
{
public function init()
{
    $this->setName('Create Device Access');
    $sort=new Zend_Form_Element_Select('employee_name');
    $sort->setLabel('Employee Name:');
    $this->addElements(array($sort));
    /* Form Elements & Other Definitions Here ... */
}

}

Now In controller action named viewDeviceAction() I have called this form as:

public function viewDeviceAction()
{
    echo 'viewDevice: ';
    $form_device=new Admin_Form_CreateDevice();
    $form_device->setMethod('post');
    $form_device->employee_name->addMultiOptions($aMembers);//here $aMembers is an array.
    $this->view->form=$form_device;
 }

Now I want following situation:
On selecting any value from above dropdown a javascript function (which resides in viewDevice.phtml ) should be called.Like in general html as:

<select id="EmployeeId" onchange="loadDeviceId();">

So I just want to khow that how to implement the onchange event on the select element in zend framework

Best Answer

That is possible to add in the server side itself. While creating your element, add the details for the onchange event like shown below.

$sort=new Zend_Form_Element_Select('employee_name',array('onchange' => 'loadDeviceId();'));

Now in your output HTML, you will see "onchange = 'loadDeviceId();'" attached to your select element.

Check my answer in an other question.

Related Topic