javascript - 从 codeigniter Controller 发送电子邮件后无法在 jquery 中获得响应

标签 javascript php jquery codeigniter email

在将发送电子邮件功能包含到 Controller 后,我没有得到响应,但代码完美地发送电子邮件,唯一的问题是没有得到响应。如果我删除发送电子邮件的代码,那么在这种情况下, Controller 的响应很好,但是当我包含电子邮件功能时,我不会在 jquery View 代码上收到响应。

我发布了我的 Controller 代码和电子邮件功能代码,还发布了我的 jQuery,以便您可以查看它。

Controller 代码: /*****创建新客户********/

public function createcustomer()
    {
        $this->form_validation->set_rules("firstname", "First Name", "required");
        $this->form_validation->set_rules("lastname", "Last Name", "required");
        $this->form_validation->set_rules("email", "Email", "required|is_unique[gab_users.email]|trim|xss_clean");
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|matches[confirmpassword]|md5');
        $this->form_validation->set_rules('confirmpassword', 'Password Confirmation', 'required|min_length[5]');

    if($this->form_validation->run() === FALSE)
    {
        die("fail");
    }
    else
    {
        $activation_code = sha1(uniqid($this->input->post('email'),'true'));
        $data = array("firstname"=>$this->input->post('firstname'), "lastname"=>$this->input->post('lastname'), "email"=>$this->input->post('email'), "password"=>$this->input->post('password'), "ip"=>$this->input->post('ip'), "activation_code"=>$activation_code, "created_on"=>now(), "active"=>1, "group"=>2);
        $verifyCreate = $this->index_model->set_user($data);

        if($verifyCreate === "success")
        {               

                if($this->sendEmail() === 'done')//If I remove this line and write only die('done'); its work perfectly. model is working fine data is saved in the database in both conditions. The big issue is response?
                {die('done');}
        }
        else
        {
            die("invalid");
        }

    }

}
**/******Send Email********/**
public function sendEmail()
{
      $this->load->library('email'); 
      $this->email->set_newline("\r\n");
      $this->email->from('email@gmail.com', 'Name');
      $this->email->to('*****@gmail.com');
      $this->email->subject(' My mail through codeigniter from localhost '); 
      $this->email->message('Hello World…');
      if (!$this->email->send()) {
        die(show_error($this->email->print_debugger())); 
        }
      else {
        die("done"); //I have also check it by return same thing is happened
      }
}

我在 View 端编写的 Jquery 代码:

<script>$().ready(function() {
    $("#regForm").validate({
        rules: 
        {
            firstname: "required",
            lastname: "required",
            email: 
            {
                required: true,
                email: true
            },
            password: 
            {
                required: true,
                minlength: 5
            },
            confirmpassword: 
            {
                required: true,
                minlength: 5,
                equalTo: "#password"
            }
        },
        messages: 
        {
            firstname: "First Name Required",
            lastname: "Last Name required",
            email: "Please enter a valid email address",
            password: 
            {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            confirmpassword: 
            {
                required: "Please provide a password confirmation",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            }
        },
        submitHandler: function(form) 
        {alert('customer register');
          $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>createcustomer",
            data: $(form).serialize(),
            timeout: 3000,
            success: function(data) {
            alert(data);
                if(data == 'done')
                {
                    $.blockUI({ css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                            } });

                            setTimeout($.unblockUI, 2000);
                    $('#successreg').show().delay(7000).fadeOut();
                    window.setTimeout(function(){
                        window.location.href = "<?php echo base_url(); ?>customer";
                    }, 7000);

                }
                else if(data == 'invalid')
                {
                   $.blockUI({ css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                            } });

                            setTimeout($.unblockUI, 2000);
                            $('#regerrormsg').show().delay(7000).fadeOut();
                    window.setTimeout(function(){
                        window.location.href = "<?php echo base_url(); ?>customer";
                    }, 7000);
                }
                else
                {
                   $.blockUI({ css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                            } });

                            setTimeout($.unblockUI, 2000);
                    $('#regerrormsg').show().delay(7000).fadeOut();
                    window.setTimeout(function(){
                        window.location.href = "<?php echo base_url(); ?>customer";
                    }, 7000);
                }
            }
          });
          return false;
        }

    });

}); </script>

请告知为什么我在使用 sendEmail 函数后,在发送电子邮件并将数据保存到数据库时没有收到 Controller 的响应,但没有收到响应,因此无法向用户显示确认消息。

最佳答案

编辑:请注意以下所有内容也都是好东西,但您的实际问题是如何从 sendMail() 函数返回。 Die 是错误的,因为它会杀死脚本(线索就在名称中),因此它不会继续允许调用脚本响应。更改为:

 ....
 if (!$this->email->send()) {
    die(show_error($this->email->print_debugger())); 
    }
  else {
    return "done"; 
  }

对于 http 响应来说,Die 并不是一个好的做法,因为它可能被解释为一个损坏的页面。为什么不返回 jQuery 喜欢的东西,例如 json 字符串。例如:

header('Content-Type: application/json');
if($verifyCreate === "success")
    {               

            if($this->sendEmail() === 'done'){
                 echo json_encode(array('status'=>'success'));
                 return;
            }
    }
    else
    {
        echo json_encode(array('status'=>'fail'));
        return;
    }

然后在你的 jQuery 中

    //redacted
    success: function(data) {
         if(data.status==='success'){
              //do some success stuff
         }
         else if(data.status==='fail'){
              //do some fail stuff
         }else{
              //do some unexpected stuff
         }
    }

您可能还需要在 $.ajax 对象中设置 contentType:json

关于javascript - 从 codeigniter Controller 发送电子邮件后无法在 jquery 中获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109507/

相关文章:

PHP proc_open 多次打开

javascript - 将 tooltip-master 与 bootstrap v3.3.5 和 jquery 1.11.3 一起使用时,文本框不显示

javascript - 使用 AJAX 调用 asp 页面,然后使用从页面返回的 JSON 数据来填充带有选项的选择框

javascript - 从用户脚本中的匿名函数获取函数指针

javascript - 如何通过 JavaScript 函数向对象添加多个键和值?

JavaScript 对象快捷方式

php - 更改 WordPress 网站的 url。

javascript - Dart Nodejs 和 Socketio

php数组和jquery添加类

javascript - 响应式导航菜单调整大小?