0
votes

Hello i'm using CakePhp i'm trying to make a chained select with Ajax, the idea is that i make the jQuery action in the event onchange of the select input

onchange="TheFunction()";

but it seems like i have some syntax problém, the idea is that i send the value of the selected item to the function ShowProvider that searches for the providers that belongs to the selected category, and then sends it back to the callback function.

echo $this->Form->input('category_id',array('empty'=>'Selectionnez une','options'=>$categories,'class'=>'category','onchange'=>'

    <?php
            echo "

            $.get(
            //premier argument, le lien vers lequel on envoie les données
            '. $this->Html->url(array('controller'=>'articles','actions'=>'ShowProvider'),true) .',
            //2eme argument, on envoie l\'id de la catégorie selectionné
            {id:$("select.category option:selected").attr("value")},

            3éme argument   
            function(data){
                alert("wssel");
            }
            )
            "
        ?>
    '));

here is the action ShowProcider()

public function ShowProvider(){
    if ($this->request->is('ajax')) {
        $result = $this->Article->Provider->find('list',
            array('conditions'=>
                    array('category_id'=>$this->request->query['id'])
            )
        );
        if ($result) {
            $ret['name']=$result['Provider']['name'];
        }
        else{
            $ret['name']='Vide.';
        }
        echo json_encode($result);
        exit();
    }
}

Thank's in Advance :)

1
That looks like little bit "self-build". Using the frameworks JsonView you could make this a lot cleaner. Also see dereuromark.de/2014/01/09/ajax-and-cakephp There is also a live example on exactly that.mark
thank's for teh help man i will try itKamal Tahir

1 Answers

0
votes

add this:

$this->layout = 'ajax';
$this->autoRender = false;

juste before

public function ShowProvider(){

and remove

exit();