Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Value of select field depend another value of select field.
  • I have two select field, values of the second depend of the value selected on the first select field, and when change value of the first second, second field change too according relations defined on ORM.

    How can i do this? an example please.

    Thanks
  • HarroHarro
    Accepted Answer
    That is a frontend operation, requiring javascript to do an ajax call to a rest controller to retrieve the related data based on the selection and after filling the dropdown, enable it.

    More a jQuery thing than a Fuel thing, and as I don't do frontend (a lot), its not really my thing. But I think there are plenty of jQuery examples to be found on the internet.
  • Hi Harro, Yes I know.

    But I want to know if fuel has a method for this.

    About AJAX, does fuel have anything to control or do?
    If we underlined the ajax query with jQuery, how do I call the controller?

    Something as ... url = '/ admin / product / sublist /'. $ Id_list ??
  • HarroHarro
    Accepted Answer
    No, Fuel doesn't provide anything frontend, as there are 1001 solutions, and everyone has his/her own perference.

    To create an API, your controller must extend either Controller_Rest (if all actions are APi methods) or Controller_Hybrid (if you mix web actions with api actions). Have the API return an array, the controller will convert that to the format requested (usually JSON).

    On API design, I can recommend Phil's book, https://apisyouwonthate.com/books/build-apis-you-wont-hate.html

    As for the jQuery, if you Google "jquery ajax populate dropdown" or "jquery ajax populate select", you get lots of examples.


  • One last question about this.

    I have this jQuery code (And i can trace it and work fine):

        function modelos_ajax(obj){
            console.log('function execute');
            $('#modelos').empty()
            var dropDown = document.getElementById("marca");
            var marca = dropDown.options[dropDown.selectedIndex].value;
            $.ajax({
                    type: "POST",
                    url: "/admin/producto",
                    data: { 'id_marca': marca  },
                    success: function(data){
                        var opts = $.parseJSON(data);
                        $.each(opts, function(i, d) {
                        $('#modelos').append('<option value="' + d.id + '">' + d.nombre + '</option>');
                        });
                    }
                });
        };

    And this into controllers:

    class Controller_Admin_Producto extends Controller_Admin{
            public function action_create(){...}

            ....

            public function post_modelos(){

            $id_marca = Input::post('id_marca');

            $modelos = Model_Marca_Modelo::find_by_marca($id_marca, array('order_by' => array('nombre' => 'asc')));
            $modelos = Arr::assoc_to_keyval($modelos, 'id', 'nombre');
            Arr::insert_assoc($modelos, array('0'=>''),0);

            echo json_encode($modelos);;

        }

    }

    And this not work, only i get errors. return an error, as if i want access an specific file.

    Any idea.
  • I told you that wouldn't work. 

    You need a Contoller_Hybrid or Controller_Rest for it. Standard controllers require View rendering, you can't just echo something out.
  • Harro, I know.

    For that reason, Controller_Admin Extends from controller Hybrid.

    I can send request url, but this return me the index and not result.

    Why?
  • HarroHarro
    Accepted Answer
    REST methods need to return an array of data. The response methods in the Hybrid or REST controllers will convert that array to the required output format.

    Note that by default, it uses the HTTP Accept header to determine the return result. Which means you can't test it with a browser, as that would as for "text/html", and an array can't be respresented in HTML (in development mode, it will use var_dump() to dump the array), so you get an error.

    Either disable Accept header checking in the config, or hardcode the return type to JSON, or use something like Postman to test your API.

    Assuming your request goes to /admin/producto/modulos/<marca_id>, that should work fine, unless you have a route that prevents that URI from being accessed.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion