4

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

here I did the same thing but when I click on any option of select , javascript function is not called and that page reloads again but no javascript function is called. Why this is occuring?When I see the page source its right as it shows: <select name="employee_name" id="employee_name" onchange="submit();">Plz suggest me.
did u check in Firebug? what is the code inside submit javascript function?
Yup I checked in firebug and on change it is not going in javascript function.This function has an alert.
Now What I did instead of echo $this->form I just copy the view source of that page and copied into phtml file and when I eliminate form tag it is actually going into javascript function here form tag is as follows: <form id="create device access" name="create device access" action="" method="post" enctype="application/x-www-form-urlencoded"> after removing form tag javascript function runs.Why??I think in my opinion since there is no action thats why form reloads.But I have to call javascript function.Now what has to be done??
you can echo only the select element like echo $sort; form tag wont appear in the HTML code
0

Since you want to implement an event handler for the onChangeevent you will have to do it in javascript. There is no native way to implement it in PHP or Zend framework as far as I know.

Using JQuery you could do something like this:

$('#employee_name').change(function() 
{
   //call your javascript function here
});

You can even call your function directly like this:

$('#employee_name').change(yourFunctionName);

Hope that helps you.

4 Comments

I know how to implement javascript with jquery and all.But the thing is that How I have to call that javascript function on selecting any value from dropdown?and that dropdown is made in Zend_form.
Sorry, but I don't get your problem. The element you created in Zend form has an id of "employee_name". You can use this id to attach the event handler to the form element. In my example if a change event is fired on the "employee_name" a callback function (yourFunctionName) will be executed.
It is actually possible to add the onChange attribute in the form class itself, see @emaillenin's answer.
@Salman Thanks for the notifier. Actually I didn't quite get what he was trying to do, but after seeing his modified post I realized that emaillenin's answer was the correct solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.