简体   繁体   中英

CodeIgniter : AJAX post data issue

i have a problem with this simple login with ajax is when i click login the page refresh when it should not -_-

My form

<div class="user_login">
            <form action="<?php echo base_url('welcome/login');?>" method="post" id="login-usr-frm">
                <label>Email / Username</label>
                <input type="text" name="user"/>
                <br />

                <label>Password</label>
                <input type="password" name="password" />
                <br />

                <div class="checkbox">
                    <input id="remember" type="checkbox" />
                    <label for="remember">Remember me on this computer</label>
                </div>

                <div class="action_btns">
                    <div class="one_half"><a href="#" class="btn back_btn"><i class="fa fa-angle-double-left"></i> Back</a></div>
                    <div class="one_half last"><input type="submit" id="lgn-btn-red" class="btn btn_red" value="Login"></div>
                </div>
            </form>

            <a href="#" class="forgot_password">Forgot password?</a>
        </div>

My jquery code

<script type="text/javascript">
    $(function (){
        $("#login-usr-frm").submit(function(event) {
            event.preventDefault();
            var url = $(this).attr('action');
            var postData = $(this).serialize();

            $.post(url, postData, function (o))

        }, 'json');

        });

    });
</script>

i can't find the issue , please help ! :)

First thing you need to start Ajax using $.ajax({ //ajax code });

And Then put message in if condition to show success and error message

Try this code below:

    $(document).ready(function (){
        $("#login-usr-frm").submit(function (e){
            e.preventDefault();
            var url = $(this).attr('action');
            var method = $(this).attr('method');
            var data = $(this).serialize();

            $.ajax({
               url:url,
               type:method,
               data:data
            }).done(function(data){
               if(data !=='')
                {
                    $("#response").show('fast');
                    $("#response").effect( "shake" );
                    $('#frm_login')[0].reset();
                }
                else
                {
                window.location.href='<?php echo base_url() ?>welcome/login';
                throw new Error('go');
                } 
            });
        });

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM