I have create a form which contain just only one input tag which name is first_name .
Now I want to show the first_name is either exist in database or not.
I have create a database name db_practice ,table which name is tbl_user and there are many field. first_name is one of the field name.
Here is my all code. Please help me to find out my problem.
This is view code:
<html>
<head>
<meta charset="UTF-8">
<title>Check username availability using jQuery, AJAX</title>
<script type="text/javascript">
$(document).ready(function() {
$("#username").live("blur", function(e) {
$('#msg').hide();
if ($('#username').val() == null || $('#username').val() == "") {
$('#msg').show();
$("#msg").html("Username is required field.").css("color", "red");
} else {
$.ajax({
type: "POST",
url: "<?php echo base_url();?>"+"/welcome/get_user",
data: $('#signupform').serialize(),
dataType: "html",
cache: false,
success: function(msg) {
$('#msg').show();
$("#msg").html(msg);
},
error: function(jqXHR, textStatus, errorThrown) {
$('#msg').show();
$("#msg").html(textStatus + " " + errorThrown);
}
});
}
});
});
</script>
</head>
<body>
<div style="margin: 10px 0 0 10px;width: 600px">
<h3>Codeigniter username availability check</h3>
<form id="signupform" style="padding: 10px;">
<fieldset>
<legend>Check username</legend>
<div>
<label>Username</label><br/>
<input type="text" name="first_name" id="username"/>
<div id="msg"></div>
</div>
</fieldset>
</form>
</div>
<script src="<?php echo assets_url(); ?>js/jquery-1.9.1.min.js"></script>
<script src="<?php echo assets_url(); ?>js/jquery-migrate-1.2.1.js"></script>
<script src="<?php echo assets_url(); ?>js/jquery-ui-1.10.3-custom.min.js">
</script>
</body>
</html>
<!-- below jquery things triggered on onblur event and checks the username availability in the database -->
This is controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('my_model');
}
public function index()
{
$this->load->view('welcome_message');
}
public function get_user()
{
if (isset($_POST['first_name']))
{
$username = $_POST['first_name'];
$results = $this->my_model->get_user($username);
if ($results === TRUE)
{
echo '<span style="color:red;">Username unavailable</span>';
}
else
{
echo '<span style="color:green;">Username available</span>';
}
}
else {
echo '<span style="color:red;">Username is required field.</span>';
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
This is model code:
<?php
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
/** * Description of my_model * * @author Masud */ class My_Model extends CI_Model {
//put your code here
public function get_user($username)
{
$this->db->select('*');
$this->db->from('tbl_user');
$this->db->where('first_name',$username);
$query = $this->db->get();
$result = $query->row();
if ($result)
{
return TRUE;
}
else
{
return FALSE;
}
} }
?>