Search code examples
javascriptcodeigniterbackbone.js

send form data to controller using backbone js


  var User = Backbone.Model.extend({
            defaults: {
                user_name: "",
                user_email: "",
                user_password: "",
            },

            initialize: function () {
                console.log('user has been intialized');
            },
            urlRoot: 'http://localhost/reddit/index.php/user_account/user'
        });

        $(document).ready(function(){
            $("#submit").click(function(){
                var email = $("#uname").val();
                var password = $("#upass").val();

           if(email==''||password=='')
                {
                    alert("Please Fill All Fields");
                }
                else
                {
// AJAX Code To Submit Form.
                    var user = new User({user_email: email, user_password:password});
                    user.fetch({
                        success: function () {
                            var view1 = new SampleView({model: user, el: $("#logged_user")});
                            view1.render();
                        }
                    });
                }
                return false;
            });
        });

this is my code. i need to send user_name and user_password to controller in codeigniter. i checked values send from the view in controller. those were null. can anyone help me ? Thanks


Solution

  • You have not share your controller code here.I think your controller must like this...try once

    public function user($email,$password)
    {
    
        echo $email."<br/>";
        echo $password;
    
        //perform operations
    
    }
    

    OR you can make jquery ajax() call like this...

    $.ajax({
        type:'POST',
        url:'http://localhost/reddit/index.php/user_account/user',
        data:{user_email: email, user_password:password},
        success:function()
        {
            var view1 = new SampleView({model: user, el: $("#logged_user")});
            view1.render();           
        }
    });
    

    And in Controller

    public function user()
    {
    
        $email = $this->input->post('user_email');
        $password = $this->input->post('user_password');
    
        //perform operations
    
    }