Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



60.9k

wrong routes i think



3.0k

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



60.9k

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.



29.9k
edited Nov '15

Hi,

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

https://docs.phalconphp.com/en/latest/reference/views.html#control-rendering-levels



3.0k

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.



3.0k
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;   
 }