conceptover

Recent News

Ajax call on dropdown change zend framework

When Drop down is changed then Ajax call to controller is used. This Ajax call hits to server and get data from database.
Here is code some of code snippet to for ajax call. This ajax call is handled in view of zend framework

  • formLabel($Form->get(‘country’));
    echo $this->formSelect($Form->get(‘country’));
    ?>

  • This code snippet is to render country dropdown. When country is selected from country then Ajax is called.
    here is ajax code

    $(document).ready(
    function()
    {
    $(‘select#country’).on(‘change’,
    function()
    {
    var country_id = $(this).val();
    $(‘select#state’).html(”);
    $(‘select#city’).html(”);
    $(‘select#subcity’).html(”);
    if(country_id!==””)
    {
    $.ajax
    ({
    url : “url(‘property’, array(‘action’ => ‘fetchStates’, ‘controller’ => ‘Property’))); ?>”,
    type : “POST”,
    data : {country_id:country_id},
    cache : false,
    success :
    function(result)
    {
    var arr = $.parseJSON(result);
    var str = ‘’;
    $.each(arr, function(key, value) {
    str += ‘’;
    });

    $(‘select#state’).html(str);
    }
    });
    }
    });
    });

    When country is selected from dropdown then controller PROPERTIES is called .and fetchCities action from this controller is called.
    Here is code of the fetchCities action that brings data from database.
    private function fetchCities()
    {
    $cList = array();
    $C = $this->getServiceLocator()->get(‘EntityManager’)->getRepository(‘AdministrationEntityCity’)->findAll();
    if (count($C))
    {
    foreach ($C as $City)
    {
    $cList[$City->getId()] = $City->getName();
    }
    }
    return $cList;
    }

    doctrine is being used in zend framework that interacts with database. getRepository function is used that uses findAll function to bring List of related cities of respective coutnry.

    Leave a Reply

    Your email address will not be published. Required fields are marked *