Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement ajax in cakephp?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.88k
    Comment on it

    Hello Everyone,
    In this blog, we will be learning about that how to use AJAX in cakePHP. There is not much difference of using Ajax with cakePHP or with HTML.
    Ajax is defined as Asynchronous JavaScript. It is a client-side scripting language that communicates to and from a server without the need for a complete page refresh. It works anywhere and is used in every programming language.

    An example of an ajax in a Cake APP:
    Copy this jQuery AJAX call in your main or custom js file that is used in your cakePHP application to submit the form.

    <script type="text/javascript">
        $(document).ready(function () {
            $('#saveForm').submit(function(){
                //serialize form data
                var formData = $(this).serialize();
                //get form action
                var formUrl = $(this).attr('action');
                
                $.ajax({
                    type: 'POST',
                    url: formUrl,
                    data: formData,
                    success: function(data,textStatus,xhr){
                            alert(data);
                    },
                    error: function(xhr,textStatus,error){
                            alert(textStatus);
                    }
                }); 
                    
                return false;
            });
        });
    </script>
    

    Note- change the URL parameter to match your setup.

    This ajax code will make an ajax request to call the method .

    In your controller, you have to define 'RequestHandler' component by which CakePHP will automatically use the Ajax layout when rendering your Ajax requests. If we define the component in appcontroller then every controller can use it so we have to define it in a correct location.

     

    AppController will look like --

    class UsersController extends AppController {
    
        public $components = array('RequestHandler');
    
        
    }

     

    After defining the component , what we need to do is simply create a form using CakePHP, make a form point its action to "/users/get", open "app/views/users/index.ctp" and add codes below:

    <?php 
    echo $form->create('User',array('action'=>'get','id'=>'form1'));
    echo $form->input('name');
    echo $form->submit('Save');
    echo $form->end(); 
    ?>

     

    After we make a form and point the form action to "users/add", we need to set up the controller action for it .

    Open "app/controllers/userscontroller.php", and add function add() as below.

    public function add(){      
      $this->autoRender=false;
      if($this->RequestHandler->isAjax()){
         Configure::write('debug', 0);
      }
        if(!empty($this->data)){
           if($this->User->save($this->data)){  
         echo 'Record has been added';
           }else{
             echo 'Error while adding record';
           }
        }
    }
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: