我如何在symfony 1.4中使用ajax(表单字段填充)

时间:2021-03-25 07:27: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.

我是symfony框架的新手。我正在尝试根据状态选择框中的选择填充city_id表单字段。

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

我在actions.class.php文件中创建了一个名为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 = ?', array($request->getParameter('stateId')));

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

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

关于状态的更改事件,javascript代码如下:

$('#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.

函数populateSelectBox只是遍历json并在city_id表单字段中填充它。

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

但是上面的请求给出了错误,并说视图getcities不存在。

Please help me do this.

请帮我这样做。

1 个解决方案

#1


1  

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).

您是否有特殊路线在路线中处理此操作?如果使用默认路由,则必须首先创建一个特殊路由,并为stateId指定特定参数。您可以在浏览器中手动测试生成的URL(您必须在操作中禁用ajax测试)。

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

你的学说请求没有执行!你可以使用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);
    //}
}

#1


1  

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).

您是否有特殊路线在路线中处理此操作?如果使用默认路由,则必须首先创建一个特殊路由,并为stateId指定特定参数。您可以在浏览器中手动测试生成的URL(您必须在操作中禁用ajax测试)。

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

你的学说请求没有执行!你可以使用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);
    //}
}