Wednesday, September 25, 2013

Re: Ajax request

For better solution do not refresh the button but only the search result.

On 25-Sep-2013 6:47 PM, <webgaertner@gmail.com> wrote:
Hello,
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 code:

Controller:
    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);
        }
       
    }

View:

<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...

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: