0

Js:

            $(document).ready(function(){
            $('#username').change(function(){
                var username = $('#username').val();
                $('#usernamemsg').html('<img src="<?php echo base_url()?>images/ajax_loader.gif"/> Checking');
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url()?>user/username_validation",
                    dataType: "json",
                    data: "username="+username,                     
                    success: function(data){
                        if(data.valid){
                            $('#usernamemsg').html(data.msg);
                        }else{
                            $('#usernamemsg').html(data.msg);
                        }
                    }
                });
            });
        });

Views:

<form id="add" method="post" action="">
        <table>
            <tr>
                <td>Username</td>
                <td><input type="text" id="username" name="username" />
                <div id="usernamemsg"></div></td></tr>      
            <tr>
                <td>Password</td>
                <td><input type="password" id="password" name="password" /></td></tr>
            <tr>
                <td><input type="submit" name="add" value="OK" id="submit" /></td></tr>
            </table></form>

Controller:

        public function index(){
            $data['title']          = 'Register';
            $data['template']       = 'outside/validation/validation';
            $this->load->view('templates/home_template',$data);

        $add = $this->input->post('add');
        if($add){
            $data                = array(
            'username'           => trim($this->input->post('username')),
            'password'           => trim($this->input->post('password')));
            $this->data_mod->insert('user',$data);
            redirect('registration/welcome');
        }
    }   
public function username_validation(){
        $user = $this->data_mod->validation('user',array('username' => $this->input->post('username')));                        
        if($user >= 1){
            $msg = array(
            'valid' => false,
            'msg'   => 'Username is already taken');
        }else{
            $msg = array(
            'valid' => true,
            'msg'   => 'Username is available');
        }
        echo json_encode($msg);
    }

* I'm trying to learn English, so i speak not well.

When i press submit, the form does not notify the username field and it submit to server althought the code of js is working correct when i'm typing a username.

Could someone help me?

3
  • you mean even though the username is not valid, the form keep collect the username and insert it to db? Commented Nov 14, 2012 at 3:45
  • Did you mean if the input is empty then you want to be notified ? Commented Nov 14, 2012 at 3:59
  • @bondythegreat yes! that is my problem. Sheikh Heera if i add that condition in js, when i submit, it does not validate the field and insert to database. Commented Nov 14, 2012 at 3:59

1 Answer 1

1

Then try this inside your js

var username = $('#username').val();
if($.trim(username).length==0)
{
    alert('Please enter user name !');
    return false;
}

Just before following line

$('#usernamemsg').html('<img src="<?php echo base_url()?>images/ajax_loader.gif"/> Checking');

and also in your success callback replace

if(data.valid){
    $('#usernamemsg').html(data.msg);
}else{
    $('#usernamemsg').html(data.msg);
}

with

$('#usernamemsg').html(data.msg);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.