将ajax表单值传递给控制器​​codeigniter

时间:2014-03-02 08:58:38

标签: php ajax codeigniter

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

            <script type="text/javascript">
                                    $(document).ready(function () {
                    $('form.ajax-form').onclick('login_submit', function() {
                        var obj = $(this), // (*) references the current object/form each time
                            url = obj.attr('action'),
                            method = obj.attr('method'),
                            data = {};

                        obj.find('[user_email]').each(function(index, value) {
                            // console.log(value);
                            var obj = $(this),
                                email = obj.attr('user_email'),
                                value = obj.val();

                            data[email] = value;
                        });

                        obj.find('[user_pass]').each(function(index, value) {
                            // console.log(value);
                            var obj = $(this),
                                pwd = obj.attr('user_pass'),
                                value = obj.val();

                            data[pwd] = value;
                        });

                        $.ajax({
                            // see the (*)
                            url: url,
                            type: method,
                            data: data,
                            success: function(response) {
                                console.log(response);
                                // $("#feedback").html(data);
                            }
                        });

                        // console.log('Trigger');
                        return false; //disable refresh
                    });
                });
            </script>
            <?php echo form_open('welcome/login', array('class' => 'ajax-form','style'=>'z-index: 202')); ?>

            <input name="user_email" type="email" class="login_email" placeholder="Email address" required autofocus>
            <br />
            <input name="user_pass" type="password" class="login_pass" placeholder="Password" required>
            <label class="checkbox">
                <a href="#" class="forgot_pass">Forgot login details?</a>
                <button class="btn btn-lg btn-info" id="login_submit" style="margin-left:20px;">Sign in</button>
            </label>
                    </form>
            <?php if(isset($login_error) && $login_error !=='') { ?><div class="alert alert-danger login_err"><?php echo $login_error; ?></div> <?php } ?>
            <!--                    </div>-->

您好我正在尝试将电子邮件和密码传递给codeigniter中的控制器。

有任何想法,请帮忙吗?

1 个答案:

答案 0 :(得分:1)

你可以通过以下方式简化大事:

$('#ajax-form').submit(function(){
 var form = $(this);
   $.ajax({
    url: form.attr('action'),
    type: form.attr('method'),
    data: form.serialize(),
    success: function(response) {
      console.log(response);
      // $("#feedback").html(data);
    }
  });
  return false;
});