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 dependebale dropdown

hy , excuse me i dont speak english very good :)

i created a dependble dropdown but i have erro in alert($.parsejson(data) return null controller action : $this->view->disable(); $response = new \Phalcon\Http\Response(); $request = new Phalcon\Http\Request(); $id_essai= $request->get('id_essai'); $query = "SELECT * FROM modalite WHERE modalite.id_essai = $id_essai"; $result_dis = $this->modelsManager->executeQuery($query); // var_dump($result_dis); //return json_encode($result_dis); return json_encode($result_dis, true);

function ajax in my view is : <script> $("#id_essai").change(function() { var value = $(this +'option:selected').val(); var getResultsUrl = '/STC4/prod/Query/moda'; $.ajax({ type: "GET", url: getResultsUrl, datatype: "json", data: {'id_essai': value}, success: function(data){ data = $.parseJSON(data); alert(JSON.stringify(data, undefined, 2)); alert($.parseJSON(data)); =======> return null for(var i=0 ; i<data.lenght; i++){ console.log(data[i]); var chaine =''; for(var i = 0 ; i<data.lenght; i++){ chaine += '<option value="'+ data[i].id_mod +'">'+ data[i].code_mod +'</option>'; } $('#id_mod').html(chaine); } } }); }); </script> please if you have any idea in this subject help me

thanks

Please format your code :)



1.6k

<script>

$("#id_essai").change(function() {

var value = $(this +'option:selected').val();

var getResultsUrl = '/STC4/prod/Query/moda';

$.ajax({
 type: "GET",
    url: getResultsUrl,
    datatype: "json",
    data: {'id_essai': value},
    success: function(data){
    data = $.parseJSON(data);
    alert(JSON.stringify(data, undefined, 2));
    alert($.parseJSON(data));  ===>>>> return  Null
    for(var i=0 ; i<data.lenght; i++){
    console.log(data[i]);
    var chaine ='';
    for(var i = 0 ; i<data.lenght; i++){
    chaine += '<option value="'+ data[i].id_mod +'">'+ data[i].code_mod +'</option>';
    }
    $('#id_mod').html(chaine);
  }
 }

}); }); </script> controlller Action ::

$this->view->disable();

           $response = new \Phalcon\Http\Response();

           $request = new Phalcon\Http\Request();

           $id_essai= $request->get('id_essai');

           $query = "SELECT * FROM modalite WHERE modalite.id_essai = $id_essai";

           $result_dis = $this->modelsManager->executeQuery($query);

      //  var_dump($result_dis);

        //return json_encode($result_dis);

        return json_encode($result_dis, true);

        if you have an idea please help me
        thanks


11.6k

first check if your route is defined as POST or GET, then if request return code 200 in your browser devtools.. in your json call, try like this:

    complete: function (response) {
            rep =response.responseTex);
            console.log( jQuery.parseJSON((rep.myKey));

if you plane to use a lot of json, implement something in your controllerBase.php:


    class ControllerBase extends Controller
        {
            public $jsonMessages = [];
            ...

            public function afterExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) 
            {
             if($this->request->isAjax() == true) {
                    $this->response->setContentType('application/json', 'UTF-8'); //--> maybe it's done by default withsetJsonContent()
                    $data = $dispatcher->getReturnedValue();
                    $this->response->setJsonContent($data);
                    return $this->response->send();
            }

then in your controller


        $this->jsonMessages['myKey'] = $my response
        return $this->jsonMessages;