【问题标题】:how to populate form select with zend framework 2 (2.3) and ajax request如何使用 zend 框架 2 (2.3) 和 ajax 请求填充表单选择
【发布时间】:2014-09-18 21:48:55
【问题描述】:

我有 3 个动态选择,state->country->city,我会将它们与 ajax 请求连接起来,但我做不到 :( 没有 zf2 更简单!

如果我在 stateCountryCityAction 中手动设置 $idState(例如 $idState = 1;),该函数可以正常工作,但实际上不起作用!为什么???? :(

这是我的应用程序\控制器:

public function stateCountryCityAction()
    {
    $form1   = new StateCountryCityForm();
    $form1->get('State')->setOptions(array('value_options'=> $this->getState()));
    $request = $this->getRequest();

    if ($request -> isXmlHttpRequest()){
        $data = $request->getPost();  
        if (isset($data['state'])){
           $idState = $data['state'];
           $form1->get('Country')->setOptions(array('value_options'=> $this->getCountry($idState))); 
        }  

    }
    $viewmodel->setTerminal(true);
    $viewmodel->setVariables(array(
                'form1' => $form1,

    ));

    return $viewmodel;
}

这是填充第二个选择的函数。它在我的应用程序\控制器中,位于其他功能下方:

public function getCountry($idState)
    {

    $dbAdapter = $this->getAdapter();
    $sql       = 'SELECT idCountry, Country FROM Country WHERE State_idState = '.$idState.' ORDER BY Country';
    $statement = $dbAdapter->query($sql);
    $result    = $statement->execute();

    $selectData = array('0' => '----- Tutta Italia -----');

    foreach ($result as $res) {
        $selectData[$res['idCountry']] = $res['Country'];
    }

    return $selectData;
}

这是我的 jquery 函数。我试图在选择更改时提醒 var 状态,并且输出正确! jquery 函数返回选择的值。

$("select#State").change(function () {
         var state = $("select#State option:selected").attr('value');
         $.post("/zf-2tutorial/public/state-country-city", { state: state }, function () {

         });
     }); 

【问题讨论】:

    标签: javascript php jquery ajax zend-framework2


    【解决方案1】:

    尝试使用此代码$idState = $request->getPost('state'); 获取帖子价值。

    在代码$data = $request->getPost(); $data 将是一个 Zend\Stdlib\ParametersInterface - 对象而不是像您的示例中的数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      相关资源
      最近更新 更多