0

I have been trying check email availibity using ajax and jquery script as follows,

my controller:

$get_result = $this->user->check_email_availablity(); if($get_result == FALSE ) { $validate['message'] = '<p>Email is not available.</p>'; } else { $validate['message'] = '<p>Email is available.</p>'; } $this->load->view('user/signup', $validate); 

my model:

function check_email_availablity() { $email = $this->input->post('u_email'); $query = $this->db->query('SELECT u_email FROM tbl_users where u_email = "'.$email.'"'); if($query->num_rows() === 1) { return FALSE; } else { return TRUE; } } 

my js:

$(document).ready(function() { /// make loader hidden in start $('#Loading').hide(); $('#email').blur(function(){ var a = $("#email").val(); var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/; // check if email is valid if(filter.test(a)){ // show loader $('#Loading').show(); $.post("<?php echo base_url()?>main/signup", { email: $('#email').val() }, function(response){ //#emailInfo is a span which will show you message $('#Loading').hide(); setTimeout("finishAjax('Loading', '"+escape(response)+"')", 400); }); return false; } }); function finishAjax(id, response){ $('#'+id).html(unescape(response)); $('#'+id).fadeIn(); } }); 

my view:

 <?php echo form_input('u_email', set_value('u_email'), 'class="form-control" id="email"'); ?> <span id="Loading"><?php echo $message; ?></span> 

My problem is model always returns TRUE and shows 'email is available' message, how do I check the email availability live

8
  • yeah I checked that way too and >0 still issue is remaining Commented Jul 28, 2015 at 3:43
  • 1
    echo $query->num_rows() and check the result .. is its return 1 ? Commented Jul 28, 2015 at 3:59
  • 1
    so $query->num_rows() give value 0 always ? Commented Jul 28, 2015 at 4:48
  • 1
    ok if($query->num_rows()) { } try like this Commented Jul 28, 2015 at 5:41
  • 1
    Let us continue this discussion in chat. Commented Jul 28, 2015 at 5:54

1 Answer 1

1

In your model change the if condition in query row:

function check_email_availablity() { $email = $this->input->post('u_email'); $query = $this->db->query('SELECT u_email FROM tbl_users where u_email = "'.$email.'"'); if($query->num_rows() > 0) { return FALSE; } else { return TRUE; } } 

And in your controller:

 $get_result = $this->user->check_email_availablity(); if(!$get_result) //if email already exist in your database { $validate['message'] = '<p>Email is not available.</p>'; } else { $validate['message'] = '<p>Email is available.</p>'; } $this->load->view('user/signup', $validate); 
Sign up to request clarification or add additional context in comments.

2 Comments

did you check if the email really exists in your database?
yeah i checked with existing emails and non existing ones

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.