【问题标题】:Form is getting submitted even if email exist in database...How to clear email field and ask user to enter non existing email ID即使数据库中存在电子邮件,也会提交表单...如何清除电子邮件字段并要求用户输入不存在的电子邮件 ID
【发布时间】:2017-08-29 07:42:11
【问题描述】:

以下代码仅在电子邮件是否存在时提醒用户。我应该如何阻止用户提交表单 //这是视图create.php

    $(document).ready(function()
    {
            $('#email').change(function()
            {
                var email = $("#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}$/;
                if(email!='')
                {
                    $.ajax({
                        type:"POST",
                        url: "<?php echo site_url('welcome/email_availabilty'); ?>",

                        data:{ email:email },
                        success:function(data)
                        {   
                            if(data)
                            {
                                alert('Email Already Exists');

                            }
                            else
                            {
                                alert('Email Available');
                            } 
                        },
                        return false;
                        error: function(e){
                            console.log(e);
                        }
                    });
                }
            });
        });

//这是控制器welcome.php

  function email_availabilty()
   {

        $res=$_POST['email'];
        $this->load->model('queries');
        $data=$this->queries->email_available($res);
        echo $data;
    }

//这是模型查询.php

public function email_available($res)  
    {

        $data=$this->db->query("select email from tbl_posts where email='$res'");
        if($data->num_rows() >= 1)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

如果电子邮件已经存在,请告诉我如何阻止用户提交表单。

【问题讨论】:

    标签: php ajax codeigniter


    【解决方案1】:

    谢谢你们! 我得到了解决方案 通过添加 '$("#email").val('');'之后

    alert('电子邮件已经存在');

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您没有通过相应地使用 event.preventDefault()returnfalsetrue 来阻止浏览器的默认行为。

      function [...](e) {
          e.preventDefault();
          [...]
      }
      

      由于您的 AJAX 代码中有随机的 return false;,您还应该遇到语法错误。

      改变,

      success:function(data) {   
          if(data) {
              alert('Email Already Exists');
      
          } else {
              alert('Email Available');
          } 
      },
      return false; <---- Here...
      

      到,

      success:function(data) {   
          if(data) {
              alert('Email Already Exists');
      
          } else {
              alert('Email Available');
          }
      
          return false; <---- Here...
      },
      

      【讨论】:

        【解决方案3】:

        如果电子邮件已经存在,您应该使用 jQuery 禁用表单的 Send 按钮。

        更好的是,您应该只在电子邮件不存在时启用它。

        【讨论】:

          猜你喜欢
          • 2014-05-22
          • 1970-01-01
          • 2015-03-02
          • 1970-01-01
          • 2016-03-22
          • 1970-01-01
          • 2018-05-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多