Solicitud de Cakephp Ajax

I have a problem with a search field on my site and I hope you can help me. I got a table called "Clients" with some information like firstname, lastname, adress etc. and all MVC files. For testing I use the Client index function. In my view file I have a input field where I want to search for clients. when I type at least 4 letter the search function should automaticaly display the client names in a div container under the search field (like the google instant search). Basically it is working, but after first search I got a second input in my clients div and after some searches it will break. I think my solution is also not the right way to do this. Here is my Controller code:

public function index($searchterm=NULL) {

    if ( $this->RequestHandler->isAjax() ) {

            $clients=$this->Client->find('list', array(
                'conditions'=>array('LOWER(Client.lname) LIKE \''.$searchterm.'%\''),
                'limit'=>500
            ));

            $this->set('clients', $clients);
    }

}

Vista

<script type="text/javascript">

    $(function() {
        $( "#element", this ).keyup(function( event ) {
            if( $(this).val().length >= 4 ) {
                $.ajax({                   
                    url: '/clients/index/' + escape( $(this).val() ),
                    cache: false,
                    type: 'GET',
                    dataType: 'HTML',
                    success: function (clients) {
                        $('#clients').html(clients);
                    }
                });
            }
        });
    });
</script>

<?php echo $this->Form->input('element', array('id'=>'element'));?>

<div id="clients">
<?php
foreach ($clients as $client) {
    echo '<br>';
    echo $client;
}
?>
</div>

I hope you can help me to improve this a little...

preguntado el 25 de septiembre de 13 a las 10:09

1 Respuestas

Try like this, it will help you.

success: function (clients) {
    $('#clients').html($(clients).filter('#clients').html());
}

Respondido el 25 de Septiembre de 13 a las 10:09

let me know if you are any query or concern. - Anil Kumar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.