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

Dependent Select Dropdown Error

I create dependent select dropdown as https://github.com/phalcon/cphalcon/wiki/Dependent-Select-Dropdown

But When I call ajax, return a whole page. ajax link is eg. "inform/search" like that. but it respond whole page. Can you suggect for me?



85.5k

wrong routes i think



5.7k

Yeap I know. I wish to call Action function. I think this example also call Action function. How can I get ajax respond?



85.5k

which phalcon version are you running

edited Nov '15

if you want to return something from controller into ajax, a simple echo json_encode(//whatever you want); will do.

If it is a bug you encountering with ajax, try console.log(); to see what is being returned.



43.9k
edited Nov '15

Hi,

with ajax action set in controller the view component to LEVEL_NO_RENDER:

https://docs.phalcon.io/en/latest/reference/views.html#control-rendering-levels



5.7k

Hi Bryan,

I already used as you say but it return whole page as follow.

if you want to return something from controller into ajax, a simple echo json_encode(//whatever you want); will do.

If it is a bug you encountering with ajax, try console.log(); to see what is being returned.

Hi, I think you can follow @le51 suggestion by doing:

$this->view->disable(); in your controller function at the start.

Hi Bryan,

I already used as you say but it return whole page as follow.

if you want to return something from controller into ajax, a simple echo json_encode(//whatever you want); will do.

If it is a bug you encountering with ajax, try console.log(); to see what is being returned.



5.7k
Accepted
answer
edited Nov '15

Thanks Bryan & le51. Now I'm done.

In Volt

<div class="col-xs-2">
    {{ select("countrycode", countries, "using":['countrycode','name'], "useEmpty":false, "class":"form-control") }}                  
</div>
<div class="col-xs-2">
    {{ select("state", dis, "using":['stateId','name'], "useEmpty":false, "class":"form-control") }}
</div>

In Javascript

$("#countrycode").change(function(event){
  var value = $(this).val();
  var getResultsUrl = 'inform/search';          
  $.ajax({
    type: "POST",
    url: getResultsUrl,
    data: {"countrycode": value},
    success: function(response){                    
      $("#state").empty();
      parsed = $.parseJSON(response);
        $.each(parsed, function(){
           $("#state").append('<option value="'+ this.stateId +'">'+ this.name +'</option>');
        });                 
      }
    });
  });

In Controller

public function searchAction()
 {
    $countrycode= $this->request->getPost("countrycode","int"); 
    $query = "SELECT * FROM state WHERE state.countrycode=$countrycode";                
    $result_dis = $this->modelsManager->executeQuery($query);
    $resData = array();
    foreach ($result_dis as $result) {
            $resData[]= array("stateId"=>$result->stateId, "name"=>$result->name);
        }
    echo json_encode($resData);
    exit;   
 }