Add dependent dropdown in SocialEngine using Ajax, JavaScript


9techblog

For adding dependent dropdown for example company, branch we need to use three files.

In my example i want to create a department which should come under company and branch,
companyname->branchname->departmentname. In create department form i need companies and branches dropdown.

Modulename/Form/CreateDepartment.php

// prepare companies
$companies = Engine_Api::_()->hierarchy()->getCompanies();
//print_r($companies);

if (count($companies)!=0){
$companies_prepared[0]= "";
foreach ($companies as $company){
$companies_prepared[$company->company_id]= $company->title;
}
//print_r($companies_prepared);

// company field
$this->addElement('Select', 'company_id', array(
'label' => 'Company',
'multiOptions' => $companies_prepared,
'onchange' => "getBranch(this.value)",
));

}

$this->addElement('Select', 'branch_id', array(
'label' => 'Branch',
'RegisterInArrayValidator' => false //keep this line to avoid "value not found in haystack" Error

));


In tpl file,
modulename/views/scripts/controllername/functionname.tpl(dept.tpl)

function getBranch(x)
{
if (x=="")
{
document.getElementById("branch_id").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{

document.getElementById("branch_id").innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("POST","hierarchys/getBranches/"+x,true); //pass to hierarchy controller getbranches function
xmlhttp.send();
}

form->render($this);?>

In controller
modulename/controllers/indexcontroller.php

public function getbranchesAction()
{

View original post 75 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s