使用骨干js将表单数据发送到控制器

时间:2016-12-29 19:45:26

标签: javascript codeigniter 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;
            });
        });

这是我的代码。我需要在codeigniter中将user_name和user_password发送到控制器。我检查了从控制器视图发送的值。那些都是空的。谁能帮我 ?谢谢

1 个答案:

答案 0 :(得分:0)

你还没有在这里分享你的控制器代码。我认为你的控制器必须这样......尝试一次

public function user($email,$password)
{

    echo $email."<br/>";
    echo $password;

    //perform operations

}

或者你可以像这样调用jquery ajax() ......

$.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();           
    }
});

在控制器

public function user()
{

    $email = $this->input->post('user_email');
    $password = $this->input->post('user_password');

    //perform operations

}