0
我有3個動態選擇,州 - >國家 - >城市,我會連接它們與ajax請求,但我不能這樣做:(沒有ZF2更簡單如何填充表單選擇與zend框架2(2.3)和AJAX請求
功能工作正常,如果我中stateCountryCityAction手動設置$ idState!(如$ idState = 1;),但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() {
});
});