Welcome Guest, Not a member yet? Register   Sign In
Request check user availability CodeIgniter jquery example code
#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:
<?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


Messages In This Thread
Request check user availability CodeIgniter jquery example code - by El Forum - 10-22-2009, 05:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB