I am totally new to using phalcon i want to create dependent dropdown in phalcon. I have tried following code tbl_personaldetails/edit.phtml
<tr>
<td align="left">
<label for="country">Country</label>
</td>
<td align="left">
<?php echo $this->tag->select(array("country",
TblCountry::find("is_active = 1"),
"useEmpty" => true,
"emptyText" => "Please select",
"using" => array("country_id", "country_name"),
));
?>
</td>
</tr>
<tr>
<td align="left">
<label for="state">State</label>
</td>
<td align="left">
<?php echo $this->tag->select(array("state",
TblState::find("is_active = 1"),
"useEmpty" => true,
"emptyText" => "Please select",
"using" => array("state_id", "state_name")
));
?>
</td>
</tr>
Javascript code
<script type="text/javascript">
$(document).ready(function()
{
$("#country").change(function()
{
var value = $(this).val();
var url = 'https://'+'<?php echo $_SERVER['HTTP_HOST']?>'+'/test/tbl_personaldetails/stateDependant';
$.ajax({
type: 'POST',
//dataType:'json', //if i uncomment this didn't get reply
url: url,
data:{country_id:value}
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
</script>
controller action
public function stateDependantAction()
{
$country_id = $_REQUEST['country_id'];
if($country_id!='')
{
$country_id=$_REQUEST['country_id'];
}
else
{
$country_id=" ";
}
$data = TblState::find(array("is_active = 1","country_id = ".$country_id));
foreach ($data as $result) {
$resData[] = array("id"=>$result->state_id, "name"=>$result->state_name);
}
print_r($resData);exit; //getting this after commenting dataType:"JSON"
echo json_encode(array('error'=>'false','state'=>$resData));
}
How can i get response on view page and pass it to state drop-down please suggest me a way Thanks in advance