We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Dependent dropdown

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



2.2k
edited Mar '14

I have gone through this but it is giving error (select class not found ) on

$nationDropDown = new Select('nation_id', Nations::find(array(
            "columns"   =>  "nation_id, nation_name",
            "cache"     =>  array("key"  =>  "nations", "lifetime" => 86400)
        )), array(
            'useEmpty'  =>  true,
            'emptyText' =>  'None Specified',
            'using'     => array('nation_id', 'nation_name')
        ));


43.9k
edited Mar '14

better use:

echo Phalcon\Tag::select('nation_id', Nations::find(array(
"columns" => "nation_id, nation_name",
"cache" => array("key" => "nations", "lifetime" => 86400)
)), array(
'useEmpty' => true,
'emptyText' => 'None Specified',
'using' => array('nation_id', 'nation_name')
));


2.2k
Accepted
answer
edited Mar '14

yes, thank you for you support i used

<?php  echo $this->tag->select(array("country",
                                    TblCountry::find("is_active = 1"),
                                    "useEmpty"  =>  true,
                                    "emptyText" =>  "Please select",
                                    "using" => array("country_id", "country_name"),
                                    ));
            ?>

and my problem is solved now