Jay
Jay

Reputation: 5084

CodeIgniter: Avoiding Refresh after Submitting Form?

I am basically submitting form values to my controller and I am using CodeIgniter Framework. However, when I send the values to my controller's function, the page gets changed to the controller and leaves the index.php (current page)

index.php:

<form action="<?php echo base_url();?>index.php/LoginController/loginuser" method="post">
            <input id="login_emailbox" name="login_emailbox" type="text" class="form-control welcome-login-email" placeholder="Email" required="">
            <input id="login_passbox" name="login_passbox" type="password" class="form-control welcome-login-password" placeholder="Password" required="">
            <button id="loginbtn" type="submit" class="btn btn-info" style="margin-left: 30px">Login</button>
</form>

LoginController.php:

class LoginController extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->view('header');
        $this->load->view('footer');

        $this->load->view('index.php');
    }


    public function loginuser(){
        echo $_POST['login_emailbox'];
        echo $_POST['login_passbox'];
    }

}

Steps I take during Runtime:

1) I browse to the index.php via => http://localhost/codeig/index.php/LoginController/index

2) Fill the form and hit submit. Values are submitted to the function: loginuser

3) Page gets redirected to the 'loginuser' function

How can I avoid this and basically send the values to the loginuser function in the controller without refreshing the current page?

Upvotes: 0

Views: 5679

Answers (2)

Mohsin Mujawar
Mohsin Mujawar

Reputation: 164

    <form>
                    <input id="login_emailbox" name="login_emailbox" type="text" class="form-control welcome-login-email" placeholder="Email" required="">
                    <input id="login_passbox" name="login_passbox" type="password" class="form-control welcome-login-password" placeholder="Password" required="">
                    <button id="loginbtn" type="submit" class="btn btn-info" style="margin-left: 30px">Login</button>
    </form>

<script>
$(document).ready(function(){
              $('#loginbtn').click(function(){
                    var login_emailbox = document.getElementById('login_emailbox').value;

                    $.ajax({
                        url:'<?=base_url()?>index.php/Controller/function',
                        method: 'post',
                        data: {login_emailbox: login_emailbox},
                        dataType: 'json',
                        success: function(response){
alert('data updated'); 
                        }
                    });
                }); 
});
</script>

Upvotes: -1

santosh
santosh

Reputation: 787

<form action="" method="post" id="myForm">
            <input id="login_emailbox" name="login_emailbox" type="text" class="form-control welcome-login-email" placeholder="Email" required="">
            <input id="login_passbox" name="login_passbox" type="password" class="form-control welcome-login-password" placeholder="Password" required="">
            <button id="loginbtn" type="submit" class="btn btn-info" style="margin-left: 30px">Login</button>
</form>

Your Jquery

 $('#loginbtn').on('click',function(e){
e.preventDefault();
    var data = $('#myForm').serialize();
    var base_url='<?php echo base_url(); ?>'
    $.ajax({
    url:base_url+'index.php/LoginController/loginuser',
    type:'POST',
    data:data,
    success:function(data){

    alert(data); // here what you want to do with response
    }
    }); 
return false;


   });

Upvotes: 5

Related Questions