Javascript 使用codeigniter和AJAX检查数据库SQL中是否存在电子邮件

Javascript 使用codeigniter和AJAX检查数据库SQL中是否存在电子邮件,javascript,php,twitter-bootstrap,codeigniter,Javascript,Php,Twitter Bootstrap,Codeigniter,我在引导模式中有一个表单,我想验证数据库中是否已经存在电子邮件。结果直接指向空白页,这个词“{有效”:false,“MSG”:“电子邮件已经被取”了。我想让味精看起来就像我在截图中看到的一样 看法 更新控制器 function isEmailExist(){ $email = $this->input->post('email'); $exists = $this->KulinerModel->isEmailExist($email); if($exists){

我在引导模式中有一个表单,我想验证数据库中是否已经存在电子邮件。结果直接指向空白页,这个词“{有效”:false,“MSG”:“电子邮件已经被取”了。我想让味精看起来就像我在截图中看到的一样

看法

更新控制器

function isEmailExist(){
$email = $this->input->post('email');
$exists = $this->KulinerModel->isEmailExist($email);
if($exists){
    echo "Email is already taken !";
}else{
     $data = array(
        'nama' =>  $this->input->post('nama'),
        'email' =>  $email,
        'password' => md5($this->input->post('password'))
    );
 $query = $this->KulinerModel->addMember($data);
 redirect('/KulinerControl/');
     }
 }
function isEmailExist()
{
   $email = $this->input->post('email');
   $exists = $this->KulinerModel->isEmailExist($email);
   if($exists){
        echo "Email is already taken";
    }else{
        echo "";
    }

 }

提前感谢

我认为实时电子邮件检查更适合您的情况

 <script>
   $(document).ready(function(){
            $('#email').keyup(function(){
            var email = $('#email').val();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url()?>KulinerControl/isEmailExist",
                data: "email="+email,                     
                success: function(response){
                        $('#msg').html(response);
                }
            });
        });
    });
</script>
防止提交一个简单的技巧

$(document).ready(function(){
    $('#myform').on('submit', function(e) {
        if ($.trim($("#msg").val()) === "") {
           alert('please choose a different email id');
            return false;
       }
    });
});
将窗体操作控制器更改为

function dataInsert()
   {
       $data = array(
        'nama' =>  $this->input->post('nama'),
        'email' => $this->input->post('email'),
        'password' => md5($this->input->post('password'))
       );
       $query = $this->KulinerModel->addMember($data);
       redirect('/KulinerControl/');
  }

我认为实时电子邮件检查更适合您的情况

 <script>
   $(document).ready(function(){
            $('#email').keyup(function(){
            var email = $('#email').val();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url()?>KulinerControl/isEmailExist",
                data: "email="+email,                     
                success: function(response){
                        $('#msg').html(response);
                }
            });
        });
    });
</script>
防止提交一个简单的技巧

$(document).ready(function(){
    $('#myform').on('submit', function(e) {
        if ($.trim($("#msg").val()) === "") {
           alert('please choose a different email id');
            return false;
       }
    });
});
将窗体操作控制器更改为

function dataInsert()
   {
       $data = array(
        'nama' =>  $this->input->post('nama'),
        'email' => $this->input->post('email'),
        'password' => md5($this->input->post('password'))
       );
       $query = $this->KulinerModel->addMember($data);
       redirect('/KulinerControl/');
  }

您需要首先停止提交表单,然后开始检查。然后,如果检查发现它是确定的,然后提交它。我将使用以下方法

$(document).ready(function(){
        $('#myform').on('submit', function(e) {
        e.preventDefault();        //<---- stop submiting the forms
        var email = $('#email').val();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()?>KulinerControl/isEmailExist",
            dataType: "json",
            data: "email="+email,                     
            success: function(data){
                if(data.valid){
                    $('#msg').html(data.msg);
                    $("#myform").submit();    //<---- submit the forms
                }else{
                    $('#msg').html(data.msg);
                }
            }
        });
    });
});
$(文档).ready(函数(){
$('#myform')。关于('submit',函数(e){

e、 preventDefault();//您首先需要停止提交表单,然后开始检查。如果检查结果为ok,则提交表单。我将使用以下方法

$(document).ready(function(){
        $('#myform').on('submit', function(e) {
        e.preventDefault();        //<---- stop submiting the forms
        var email = $('#email').val();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()?>KulinerControl/isEmailExist",
            dataType: "json",
            data: "email="+email,                     
            success: function(data){
                if(data.valid){
                    $('#msg').html(data.msg);
                    $("#myform").submit();    //<---- submit the forms
                }else{
                    $('#msg').html(data.msg);
                }
            }
        });
    });
});
$(文档).ready(函数(){
$('#myform')。关于('submit',函数(e){


e、 预防默认值();//那么具体的问题是什么?@charlietfl我想让消息显示出来,就像我在屏幕截图中附加的一样。sirso,这是一个CSS问题,它没有解释具体问题是什么以及当前发生了什么。请注意,您需要在提交中阻止默认浏览器提交handler@highcal你确实解决了你的问题?那么具体的问题是什么?@charlietfl我想让消息显示出来,就像我在截图sirso中附加的一样。这是一个CSS问题,它没有解释具体的问题是什么以及当前发生了什么。请注意,您需要在提交中阻止默认浏览器提交handler@highcal你解决了你的问题吗?谢谢r响应,先生,您的代码工作正常。但仍然可以提交。是否有任何方法在错误出现时阻止提交,或在提交时触发函数而不刷新/重定向页面?@highcal我编辑了答案,请重试。注释如果不起作用,我更新了控制器,先生,我需要将数据插入数据库,我f错误未发生。当使用您的阻止提交代码时,它不能正常工作。抱歉,我以前没有包含它。thanks@highcal由于您需要将数据添加到数据库中,现在我的控制器将无法工作(即没有数据插入模块)。我的控制器用于实时验证电子邮件。创建另一个post action controller功能。我编辑了答案,请检查。感谢您的响应,先生,您的代码工作正常。但它仍然可以提交。是否有任何方法可以在错误开启时阻止提交,或在提交时触发该功能,而无需刷新/重定向page?@highcal我编辑了答案,请再试一次。如果不起作用,请发表评论。我更新了控制器,先生,如果没有发生错误,我需要将数据插入数据库。使用“阻止提交”代码时,它不起作用。很抱歉,我以前没有包含它。thanks@highcal由于您需要将数据添加到数据库中,现在我的控制器将不会工作(即无数据插入模块)。我的控制器用于实时验证电子邮件。创建另一个后期操作控制器功能。我编辑了答案,请检查。需要添加“$(“#myform”)。取消绑定(“提交”);”。但效果很好,先生感谢添加“$(“#myform”)。取消绑定(“提交”)。但效果很好,先生,谢谢