检查文本框中输入的值是否存在于数据库中,而无需在 codeigniter 中提交表单
check whether value entered in textbox exist in database without form submit in codeigniter
我有一个有很多字段的表单,其中一个就像当用户在输入字段中输入特定代码时我需要检查该代码是否存在于数据库中而不提交表单上的 form.Because提交的数据将被插入到另一个 table.Here table 中,我需要从中检查代码是否不同。如果代码退出,如果应该在文本框旁边显示该代码是正确的,否则 invalid.Can 有人给我提供相同的示例或解决方案....在此先感谢
MY_View:
<input type="text" name="ref_code" id="ref_code" onblur="checkCodeStatus()" size=18 maxlength=6 required>
<script type="text/javascript">
function checkCodeStatus(){
var ref_code=$("#ref_code").val();
$.ajax({
type:'post',
url:'<?php echo site_url('mypage/check_code/'); ?>',
data:{ref_code: ref_code},
success:function(msg){
alert(msg);
}
});
}
</script>
在您的控制器中,您将创建函数 check_code
。
function check_code(){
$this->db->where('ref_code', $this->input->post('ref_code'));
$query = $this->db->get('table_name');
if($query->num_rows() >= 1){
echo 'value does exist';
}
}
然后,在您的 ajax 调用中,您必须执行如下操作:
$.ajax({
type:'post',
url:'<?php echo site_url('mypage/check_code/'); ?>',
data:{ref_code: ref_code},
success:function(msg){
if(msg.indexOf('value does exist') > -1)
$('#msg').html('<span style="color: green;">'+msg+"</span>");
else $('#msg').html('<sapn style="color:red;">Value does not exist</span>');
}
});
}
编辑:您只需在输入附近添加一个 HTML 元素,如下所示:
<div id="msg"></div>
我有一个有很多字段的表单,其中一个就像当用户在输入字段中输入特定代码时我需要检查该代码是否存在于数据库中而不提交表单上的 form.Because提交的数据将被插入到另一个 table.Here table 中,我需要从中检查代码是否不同。如果代码退出,如果应该在文本框旁边显示该代码是正确的,否则 invalid.Can 有人给我提供相同的示例或解决方案....在此先感谢
MY_View:
<input type="text" name="ref_code" id="ref_code" onblur="checkCodeStatus()" size=18 maxlength=6 required>
<script type="text/javascript">
function checkCodeStatus(){
var ref_code=$("#ref_code").val();
$.ajax({
type:'post',
url:'<?php echo site_url('mypage/check_code/'); ?>',
data:{ref_code: ref_code},
success:function(msg){
alert(msg);
}
});
}
</script>
在您的控制器中,您将创建函数 check_code
。
function check_code(){
$this->db->where('ref_code', $this->input->post('ref_code'));
$query = $this->db->get('table_name');
if($query->num_rows() >= 1){
echo 'value does exist';
}
}
然后,在您的 ajax 调用中,您必须执行如下操作:
$.ajax({
type:'post',
url:'<?php echo site_url('mypage/check_code/'); ?>',
data:{ref_code: ref_code},
success:function(msg){
if(msg.indexOf('value does exist') > -1)
$('#msg').html('<span style="color: green;">'+msg+"</span>");
else $('#msg').html('<sapn style="color:red;">Value does not exist</span>');
}
});
}
编辑:您只需在输入附近添加一个 HTML 元素,如下所示:
<div id="msg"></div>