user2898447
user2898447

Reputation: 15

Codeigniter Jquery username exist

I think CI return all page, i need string "GOOD".

This is a picture. http://d.pr/i/h4oi please help me) this is my VIEW on Codeigniter:

<html>
<head>
<script src="<?php echo base_url(); ?>js/jquery.js" type="text/javascript"></script>
</head>
<body>

<input type="text" id="username" /> 
<input type="button" id="check" value="Check" />
<div id="name_feedback"></div>

<script>
$("#check").click(function() {
    $.post("<?php echo base_url(); ?>main/about", { name : $("#username").val() }, function(data) {
        $("#name_feedback").html(data);
        console.log(data.length);
    } );
} );
</script>

</body>
</html>

Controller:

function about() 
    {
        if (isset($_POST['name'])) {
            $name = $_POST['name'];
            $this->db->where('username', $name);
            $users = $this->db->get('users');
            if ($users->num_rows() > 0) {
                echo "Bad";
            } else 
            {
                echo "Good";
            }
        }
        $this->load->view('about_view');
    }

Codeigniter returns all code page in #name_feedback with Answer "Good or Bad"

This is the console.log:

Good<script src="http://localhost/project/js/jquery.js" type="text/javascript"></script>
<html>
<head>
<script src="http://localhost/project/js/jquery.js" type="text/javascript"></script>
</head>
<body>

<input type="text" id="username" /> 
<input type="button" id="check" value="Check" />
<div id="name_feedback"></div>

<script>
$("#check").click(function() {
    $.post("http://localhost/project/main/about", { name : $("#username").val() }, function(data) {
        $("#name_feedback").html(data);
        console.log(data);
    } );
} );
</script>

</body>
</html>

Upvotes: 0

Views: 770

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

You don't need to load whole view in ajax request just echo the your result and call die() or exit

function about() 
    {
        if (isset($_POST['name'])) {
            $name = $_POST['name'];
            $this->db->where('username', $name);
            $users = $this->db->get('users');
            if ($users->num_rows() > 0) {
                echo "Bad";
            } else 
            {
                echo "Good";
            }
             //check for post
            die();// or exit
        }

       // $this->load->view('about_view');
    }

Upvotes: 1

Related Questions