【问题标题】:check whether value entered in textbox exist in database without form submit in codeigniter检查在文本框中输入的值是否存在于数据库中而无需在 codeigniter 中提交表单
【发布时间】:2015-09-10 10:40:29
【问题描述】:

我有一个包含许多字段的表单,其中一个就像当用户在输入字段中输入特定代码时,我需要在不提交表单的情况下检查该代码是否存在于数据库中。因为在表单上提交数据会被插入到另一个表中。我需要检查代码的表是不同的。如果代码退出,如果应该在文本框旁边显示代码是正确的,否则无效。有人可以给我提供相同或解决方案的例子....提前谢谢

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>

【问题讨论】:

    标签: mysql ajax codeigniter validation textbox


    【解决方案1】:

    在您的控制器中,您将创建函数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>
    

    【讨论】:

    • 您可以使用带有函数回调的表单验证来实现相同的范围。 ellislab.com/codeigniter/user-guide/libraries/…
    • 我不想刷新页面。我希望该消息显示在文本框旁边,而不是警报。我怎样才能做到这一点? @webcrazymaniac
    • 我已经在成功函数中这样显示了:success: function(data) { $('#msg').html(data).fadeIn('slow'); }
    • 你得到了你需要的一切。只需将输入标签内的onblur="checkCodeStatus()" 更改为onchange="checkCodeStatus()"
    • 这是另一个问题吗? alert() 显示消息,不是吗?有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多