我有3个动态选择,state-> country-> city我会用ajax请求连接它们,但我不能这样做:(没有zf2更简单!
如果我在stateCountryCityAction(ex $ idState = 1;)中手动设置$ idState,该函数可以正常工作但是dinamically不起作用!为什么???? :(
这是我的应用程序\控制器:
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的值。
$("select#State").change(function () {
var state = $("select#State option:selected").attr('value');
$.post("/zf-2tutorial/public/state-country-city", { state: state }, function () {
});
});
答案 0 :(得分:0)
尝试使用此代码$idState = $request->getPost('state');
获取帖子后的值。
在代码$data = $request->getPost();
中,$ data将是Zend \ Stdlib \ ParametersInterface - 对象而不是样本中的数组。