Welcome Guest, Not a member yet? Register   Sign In
Request check user availability CodeIgniter jquery example code
#1

[eluser]jiahui[/eluser]
I am very new in codeIgniter. I have read quite many articles about codeIgniter jquery in these 2 weeks but nothing help to solve my problem. Any kindness expert who willing to help me work it out?

This is my working coding:

I write following coding in my view/index.php

Code:
User Name : <input name="username" type="text" id="username" value="" maxlength="15" />
<span id="msgbox" style="display:none"></span>

in my controller I did include:

Code:
class Accounts extends MY_Controller {

    function Accounts()
    {
        parent::MY_Controller();
        $this->load->model('usercontacts_model');
    }

    // --------------------------------------------------------------------

    function index($message = '')
    {


        $data['extraHeadContent']= "[removed][removed]\n";
$data['extraHeadContent'].= "[removed][removed]\n";
        $data['extraHeadContent'].= "&lt;link type=\"text/css\" rel=\"stylesheet\" href=\"". base_url()."css/account.css\" /&gt;\n";
        
        
        $data['page_title'] = $this->lang->line('menu_accounts');
        
        $data['accounts'] = $this->usercontacts_model->get_admin_contacts();
        
        $this->load->view('accounts/index', $data);

    }

    // --------------------------------------------------------------------
    
    function check_user_availability()
    {
        $username = $this->input->post('username');
        
        //  Developed by Roshan Bhattarai
        //  Visit http://roshanbh.com.np for this script and more.
        //  This notice MUST stay intact for legal use
        
        //this varible contains the array of existing users
        $existing_users=array('ss','rr','ii');
        //value got from the get metho
        $user_name= $username;
        
        //checking weather user exists or not in $existing_users array
        if (in_array($user_name, $existing_users))
        {
            //user name is not availble
            echo "no";
        }
        else
        {
            //user name is available
            echo "yes";
        }
    }

In blur.js:

Code:
$(document).ready(function()
{
    $("#username").blur(function()
    {
        //remove all the class add the messagebox classes and start fading
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
        //check the username exists or not from ajax

/*the function always stop at here cannot pass thru the username value to check_user_availability page. Anything I did wrong here??*/
                
        $.post("/accounts/check_user_availability",{ user_name:$(this).val() } ,function(data)
        {
          if(data=='no') //if username not avaiable
          {
              $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
            {
              //add message and change the class of the box and start fading
              $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
            });        
          }
          else
          {
              $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
            {
              //add message and change the class of the box and start fading
              $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
            });
          }
                
        });

    });
});
#2

[eluser]Mackstar[/eluser]
Please tell us what you are wanting to do, before we have to go through your code to work it out...

Cheers

Richard
#3

[eluser]jiahui[/eluser]
Thanks for you fast reply. I'd like use jquery method to pass $username (in blur.js) to user_check_availability.php page to process whether is the username available to use or not.
#4

[eluser]Mackstar[/eluser]
I would check your controller method "check_user_availability" first and checking that this is correct, use GET to do this. once you are confident this works then it will be JQuery you need to look at.

You can check the incoming ajax request using Firefox - firebug or HttpFox plugin, if the returning data is correct, then you can look into your post call

$.post("/accounts/check_user_availability",{ user_name:$(this).val() } ,function(data)

if this is OK then check your fading in function

Just check each step until you can find out what part of the process is not working.

Hope it helps

Cheers

Richard
#5

[eluser]jiahui[/eluser]
I had downloaded firebug to test but nothing work to me. Again alert every line of my script also get nothing error there. try use Get instead of Post. but it nothing help to me...

I'm getting crazy. What I did wrong??

Richard would you be kind to provide an example to check user_availability coding by using this codeigniter framework?

You kindness will be much appreciated.......
#6

[eluser]jiahui[/eluser]
I had downloaded firebug to test but nothing work to me. Again alert every line of my script also get nothing error there. try use Get instead of Post. but it nothing help to me...

I tried to echo username which in check_user_availability.php, but it nothing happen there.
Cannot echo out but also no errors there...

I'm getting crazy. What I did wrong??

Richard would you be kind to provide an example to check user_availability coding by using this codeigniter framework?

Your kindness will be much appreciated.......[/quote]
#7

[eluser]Mackstar[/eluser]
Hi I managed to get it working, the JS is as follows
Code:
$(document).ready(function() {
    $("#username").blur(function() {
        //remove all the class add the messagebox classes and start fading
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
        //check the username exists or not from ajax


        $.post("accounts/check_user_availability",{ username:$(this).val() } ,function(data) {
            if(data=='no') {
                $("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox

                    //add message and change the class of the box and start fading
                    $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
                });

            }
            else {
                $("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox

                    //add message and change the class of the box and start fading
                    $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
                });
            }
        });

    });

});

Also note that you had user_name being posted but the CI controller looking for username. So I have changed it in the js, which should now work.

FYI I have my other files as accounts.php
Code:
&lt;?php

class Accounts extends Controller {

    function Accounts()
    {
        parent::Controller();
        //$this->load->model('usercontacts_model');
    }

    // --------------------------------------------------------------------

    function index($message = '')
    {


        $data['extraHeadContent']= "[removed][removed]\n";
$data['extraHeadContent'].= "[removed][removed]\n";
        
        
        $data['page_title'] = $this->lang->line('menu_accounts');
        
       // $data['accounts'] = $this->usercontacts_model->get_admin_contacts();
        
        $this->load->view('index', $data);

    }

    // --------------------------------------------------------------------
    
    function check_user_availability()
    {
        $username = $this->input->post('username');
        
        //  Developed by Roshan Bhattarai
        //  Visit http://roshanbh.com.np for this script and more.
        //  This notice MUST stay intact for legal use
        
        //this varible contains the array of existing users
        $existing_users=array('ss','rr','ii');
        //value got from the get metho
        $user_name= $username;
        
        //checking weather user exists or not in $existing_users array
        if (in_array($user_name, $existing_users))
        {
            //user name is not availble
            echo "no";
        }
        else
        {
            //user name is available
            echo "yes";
        }
    }
}


and views/index.php as
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
[removed][removed]
[removed][removed]
&lt;/head&gt;

&lt;body&gt;
&lt;form&gt;

User Name :
&lt;input name="username" type="text" id="username" value="" maxlength="15" /&gt;
<span id="msgbox" style="display:none"></span>
<div id="msgbox"></div>
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
This seemed to work fine...

Cheers

Richard
#8

[eluser]jiahui[/eluser]
Bravo!! It's work perfectly. I love you Richard!! ;-P
#9

[eluser]jiahui[/eluser]
halo, Richard.

I got question again. Hope you can help me too.
This time I'd like to connect database (controller) to check the availability status. In order to do that I changed my code as the following:

Code:
$this->db->where('username', $username);
$query = $this->db->get('user');
        
if ($query->num_rows() == 0)
{
    echo "yes";
}
else
{
    echo "no";
}

It doesn't work...

But if I write the coding in this way, it can be worked. Could you tell me what's the problem it is?

Code:
$SQL="select * from user where username='".$username."'";
$rs = mysql_query($SQL) or die(mysql_error());
                
if(mysql_num_rows($rs) > 0)
{
        echo "no";
}
else
{
    echo "yes";
}




Theme © iAndrew 2016 - Forum software by © MyBB