开发者

how do i use ajax in symfony 1.4 (form-field population)

开发者 https://www.devze.com 2023-03-14 13:32 出处:网络
I am new to symfony framework. I am trying to populate the city_id form-field based on the selection done in the states select box.

I am new to symfony framework. I am trying to populate the city_id form-field based on the selection done in the states select box.

I created a new method in the actions.class.php file named getStates()

here's the code for it.

public function executeGetCities(sfWebRequest $request)
{
    $this->forwardUnless($query = $request->getParameter('stateId'), 'users', 'index');

    $this->states = Doctrine_Query::create()
                    ->from('States s')
                    ->where('s.id开发者_StackOverflow = ?', array($request->getParameter('stateId')));

    if($request->isXmlHttpRequest())
    {
        echo json_encode($this->states);
    }
}

on the change event of states the javascript code is as follows:

$('#users_states').change(function(){
            populateSelectBox('users_city_id', 'get', '<?php echo url_for('states/getCities/'); ?>', {stateId:$(this).val()})
        });

the function populateSelectBox simply iterates through the json and populates it in the city_id form-field.

But the above request gives error and says view getcities doesnot exists.

Please help me do this.


Do you have a special route to handle this actio in your routing ?
If you use default routing, you have to create a special route first, with specific parameter for stateId. You can test manually the url generated, in your browser (you must disabled the ajax test in your action).

And your doctrine request is not executed ! you can return an array with fetchArray :

public function executeGetCities(sfWebRequest $request)
{
    //$this->forwardUnless($query = $request->getParameter('stateId'), 'users', 'index');

    $this->states = Doctrine_Query::create()
                    ->from('States s')
                    ->where('s.id = ?', array($request->getParameter('stateId')))->fetchArray();

    //if($request->isXmlHttpRequest())
    //{
        echo json_encode($this->states);
    //}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消