Welcome Guest, Not a member yet? Register   Sign In
AJAX check user name script
#1

[eluser]mabright[/eluser]
Hello All,

I am implementing a AJAX based check username script(papermashup.c o m/demos/check-username/) and I cannot get it to function properly.

Below is my code. When I keyup on my user name field, nothing happens. I put my AJAX code in a function inside an AJAX controller. Am I using the AJAX call correctly with the controller?

AJAX COntroller
Code:
<?php
class Ajax extends Public_Controller {

    public function __construct()
    {
        parent::__construct();
        
        $this->load->model('User_model');
    }

    public function checkhandle()
    {
        $result = 0;
        $result = $this->User_model->check_username($this->input->post('username'));
        return $result;
    }

}
/* End of file Ajax.php */
/* Location: /app/controllers/ajax.php */

JavaScript
Code:
[removed][removed]

                                [removed]
                                                        $(document).ready(function(){
                                                        $('#username').keyup(username_check);
                                                        });
                                                            
                                                        function username_check(){    
                                                            var username = $('#username').val();
                                                            if(username == "" || username.length < 6){
                                                                $('#username').css('border', '2px #CCC solid');
                                                                $('#tick').hide();
                                                            }else{
                                                                jQuery.ajax({
                                                                   type: "POST",
                                                                   url: "http://mydomain.com/ajax/checkhandle",
                                                                   data: 'username='+ username,
                                                                   cache: false,
                                                                   success: function(response){
                                                                if(response == 1){
                                                                    $('#username').css('border', '2px #C33 solid');    
                                                                    $('#tick').hide();
                                                                    $('#cross').fadeIn();
                                                                    }else{
                                                                    $('#username').css('border', '2px #090 solid');
                                                                    $('#cross').hide();
                                                                    $('#tick').fadeIn();
                                                                }
                                                                }
                                                                });
                                                            }
                                                        }
                                                        [removed]
                                                        
                                                        &lt;style&gt;
                                                        #tick{
                                                            display:none
                                                        }
                                                        #cross{
                                                            display:none
                                                        }
                                                        &lt;/style&gt;

User Sign Up View
Code:
<li>
                    <label class="label_required">Nick Name/Handle:</label>
                    <em>&lt;?php echo img('img/required_wht.gif'); ?&gt;</em>
                    &lt;input type="text" name="username" id="username" value="&lt;?php echo set_value('username'); ?&gt;" tabindex="1" size="25" maxlength="50"/&gt;
                    <span class="input_note_right">
                        Min 6 characters.
                        <img id="tick" src="&lt;?php echo base_url().'img/'; ?&gt;tick.png" width="16" height="16"/>
                        <img id="cross" src="&lt;?php echo base_url().'img/'; ?&gt;cross.png" width="16" height="16"/>
                    </span>
                    &lt;?php echo form_error('input_handle'); ?&gt;
                </li>
#2

[eluser]nuwanda[/eluser]
One thing. Your ajax data option should be like this:

Code:
data: ({name: value})
#3

[eluser]cideveloper[/eluser]
first thing you need to do is get firebug for firefox. This will allow you to test if your js is working properly.
Use the console tab - All to see if the javascript is sending the ajax call if username.length >= 6.

Also what do you mean nothing happens? For me, the border color change does work but I am always getting a null response form your url.
Nothing is being echoed. Simply returning $result will not echo anything from the controller.

I would make the following changes to your controller.


AJAX Controller
Code:
public function checkhandle()
    {
        $result = $this->User_model->check_username($this->input->post('username'));
    // Have the model set to either 1 or 0
    echo $result;
    }
#4

[eluser]mabright[/eluser]
It was my controller. Thanks for the input.




Theme © iAndrew 2016 - Forum software by © MyBB