uneeb meer
uneeb meer

Reputation: 692

controller not sending data back to ajax request codeigniter

I have developed a login system using ajax the problem is when i send the ajax request everything is working and validating fine i just need to pass data back to my ajax request I am using echo json_encode("true"); but somehow it is just echoing the value true in the controller and not going back in the view!

HTML

  <form onsubmit="return validate()" method="post" action="<?php echo base_url(); ?>admin/admin_login">
        <input class="md-input" placeholder="username" type="text" name = 'login_username'  id = 'login_username' />
        <input class="md-input" placeholder="password" type="password" name= 'login_password'  id= 'login_password' />
        <button type='submit' class="btn btn-primary btn-block btn-large">Login</button>
    </form>

AJAX

<script>

    function validate(){
        if(!$("#login_username").val()){
            alert("username is required");
            return false;
        }
        if(!$("#login_password").val()){
            alert("Password is required");
            return false;
        }
        return true;
        var data={
            "login_username" : $("#login_username").val(),
            "login_password" : $("#login_password").val()
        };
        $.ajax({
            type: 'post',
            url: '<?=base_url()?>Admin/admin_login',
            dataType: 'json',
            data:data,
            success: function (data) {
                if(data=="true"){
                    alert("ok");
                }
                else
                {
                    alert("not ok");
                }
            }
        });
    }
</script>

admin_login controller

 public function admin_login(){
        $data = $this->input->post();
        $status=$this->admin_validate->validate($data);
        if($status){
            $session=array(
                "admin"=>$this->input->post("login_username"),
            );

            $this->session->set_userdata($session);
            //redirect("Admin/contact");
            header('Content-Type: application/json');
            echo json_encode("true");
        }
        else
        {
            header('Content-Type: application/json');
            echo json_encode("false");
            //redirect("Admin");
        }
    }

Upvotes: 0

Views: 779

Answers (3)

Dinesh Kumar
Dinesh Kumar

Reputation: 649

Now iam going to change the code little

change the HTML form to

<form>
        <input class="md-input" placeholder="username" type="text" name = 'login_username'  id = 'login_username' />
        <input class="md-input" placeholder="password" type="password" name= 'login_password'  id= 'login_password' />
        <button type='button' onclick="validate()" class="btn btn-primary btn-block btn-large">Login</button>
    </form>

and Now change your ajax to

<script>

    function validate(){
        if(!$("#login_username").val()){
            alert("username is required");
            return false;
        }
        if(!$("#login_password").val()){
            alert("Password is required");
            return false;
        }


        $.ajax({
            type: 'post',
            url: '<?php echo base_url()."Admin/admin_login"; ?>',
            data:{ "login_username" : $("#login_username").val(), "login_password" : $("#login_password").val() },
            success: function (data) {
                if(data=="true"){
                    alert("ok");
                }
                else
                {
                    alert("not ok");
                }
            }
        });
    }
</script>

and your controller to

public function admin_login(){
        $data = $this->input->post();
        $status=$this->admin_validate->validate($data);
        if($status){
            $session=array(
                "admin"=>$this->input->post("login_username"),
            );

            $this->session->set_userdata($session);

            echo "true";
        }
        else
        {
            echo "false";   
        }
    }

Hope this helps you. :)

Upvotes: 2

Dinesh Kumar
Dinesh Kumar

Reputation: 649

you are returning true in the script so the form is get submitted. no ajax call occurs.

<script>

    function validate(){
        if(!$("#login_username").val()){
            alert("username is required");
            return false;
        }
        if(!$("#login_password").val()){
            alert("Password is required");
            return false;
        }
        return true;  // HERE THE ISSUE //
        var data={
            "login_username" : $("#login_username").val(),
            "login_password" : $("#login_password").val()
        };
        $.ajax({
            type: 'post',
            url: '<?=base_url()?>Admin/admin_login',
            dataType: 'json',
            data:data,
            success: function (data) {
                if(data=="true"){
                    alert("ok");
                }
                else
                {
                    alert("not ok");
                }
            }
        });
}

Upvotes: 0

Gerson E. Aguirre
Gerson E. Aguirre

Reputation: 877

Have you tried sending true or false without the quotation marks?, if not try creating an array and then passing it to the echo json_encode(); something like:

$result = array();
array_push($result, true);

echo json_encode($result);

on your ajax you will have to read it as follow

if(data[0] == true){
    alert("Ok");
}else{
    alert("Not OK");
}

Hope it helps

Upvotes: 0

Related Questions