我创建的表单只包含一个名为first_name的输入标记。
现在我想显示first_name是否存在于数据库中。
我创建了一个数据库名称db_practice,表名是tbl_user,并且有很多字段。 first_name是字段名称之一。
这是我的所有代码。请帮我解决一下我的问题。
这是查看代码:
<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 -->
这是控制器代码:
<?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 */
这是型号代码:
<?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;
}
} }
?>
答案 0 :(得分:0)
修正:
url: "<?php echo base_url(); ?>"+"/welcome/get_user",
$results = $this->my_model->get_user($username); // not My_model, it's my_model
class My_model extends CI_Model { // My_model not My_Model
$result = $query->row(); // $query, not $query_result
答案 1 :(得分:0)
进行这些更改并查看。
在Ajax代码中,url: "<?php echo base_url();?>welcome/get_user",
ajax代码段放完后
return false;
添加后,您的javascript将是这样的。
<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);
}
});
return false;
}
});
});
</script>
希望这有效。
答案 2 :(得分:0)
我发布了一个问题,其中输入标记包含来自用户的用户名。如果此用户名已经存在于数据库中,那么它将显示用户名不可用且用户名不存在于数据库中,然后显示用户名可用。我共享其解决方案。 ..............
此视图代码
<html>
<head>
<meta charset="UTF-8">
<title>Check username availability using jQuery, AJAX</title>
<script type="text/javascript">
<!--
//Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;
//Check if we are using IE.
try {
//If the Javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
//alert ("You are using Microsoft Internet Explorer.");
} catch (e) {
//If not, then use the older active x object.
try {
//If we are using Internet Explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//alert ("You are using Microsoft Internet Explorer");
} catch (E) {
//Else we must be using a non-IE browser.
xmlhttp = false;
}
}
//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
//alert ("You are not using Microsoft Internet Explorer");
}
function makerequest( user_name,objID)
{
if (user_name.length == 0) {
document.getElementById("txtHint").innerHTML = "Username available";
return;
} else{
//var user_name = document.getElementById('username').value;
serverPage='welcome/valid_user_name/'+user_name;
//alert(serverPage);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function()
{
//alert(xmlhttp.readyState);
//alert(xmlhttp.status);
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById(objID).innerHTML = xmlhttp.responseText;
//document.getElementById(objcw).innerHTML = xmlhttp.responseText;
}
}
}
xmlhttp.send(null);
}
//-->
</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" onkeyup="makerequest(this.value,'msg')"/>
<span id="msg"></span>
</div>
<div>
<label>Email Address</label><br/>
<input type="email" name="email_address" id="email_address"/>
</div>
</fieldset>
</form>
</div>
</script>
</body>
</html>
此控制器代码
<?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 valid_user_name($user_name)
{
$result = $this->my_model->get_user($user_name);
if ($result === TRUE)
{
echo '<span style="color:red;">Username unavailable</span>';
}
else
{
echo '<span style="color:green;">Username available</span>';
}
}
}
此模型的代码
<?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($user_name)
{
$this->db->select('*');
$this->db->from('tbl_user');
$this->db->where('first_name',$user_name);
$query = $this->db->get();
$result = $query->row();
if ($result)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
?>
证明了这些代码。任何人都可以使用它..