在这里使用ajax我点击忘记按钮并使用codeigniter发送邮件到特定的电子邮件

时间:2017-06-14 06:05:57

标签: html ajax codeigniter

在这里使用ajax我点击忘记按钮并发送邮件到特定的电子邮件,但我收到错误失败什么是问题我没有得到请任何人帮我下面我显示表格和控制器

表格

<div class="field-wrap">
    <label class="view-label">Email Address</label>
    <input type="email" placeholder="Email Address" name="email" id="email" class="input-control inputstyle" value="<?php echo set_value('email'); ?>"/>
    <span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<!--<div id='errorDiv' class='col-xs-12 pull-right'> </div>-->
<div class="field-wrap">
    <input type="password" placeholder="Password" class="inputstyle" name="password" id="password" value="<?php echo set_value('password'); ?>"/>
    <span class="text-danger"><?php echo form_error('password'); ?></span>

    <a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-forgot" >Forgot ?</a>  

</div>

的javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $("#btn-show-forgot").click(function () {
            //e.preventDefault();
            var email = $("#email").val();
            //  var password= $("#password").val();


            $.ajax({
                type: "POST",
                url: "<?php echo base_url() ?>" + "index.php/Login_cntrl/sendmail",
                data: {email: email},
                success: function (data)
                {
                    alert('Successfully send a mail');
                },
                error: function ()
                {
                    alert('fail');
                }
            });
        });
    });
</script>

控制器

public function sendmail() {


        $this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('login');
        } else {

            $data['email'] = $this->input->post('email');

            $findemaill = $this->Login_model->getUserInfoByEmail($data['email']);



            if (!empty($findemaill)) {
               // $this->Login_model->getUserInfo('$id');
               // $this->session->set_userdata('forgot_password_flag', 'yes');
                $emailTo = $this->input->post('email');
                   $this->session->set_userdata('findemaill',  $emailTo);
                  //  $this->session->userdata('findemaill');

                $data['result'] = $this->Login_model->forgot_pass_retrive($emailTo);
                $id = $data['result']->id;
                $this->session->set_userdata('idd', $id);



                $six_digit_random_number = mt_rand(100000, 999999);

                $this->session->set_userdata('otp', $six_digit_random_number);

                $this->load->model('Login_model');

                $pass['pass'] = $this->Login_model->forgot_pass_retrive($emailTo);

                $config['protocol'] = 'smtp';

                $config['smtp_host'] = 'ssl://smtp.gmail.com';

                $config['smtp_port'] = '465';

                $config['smtp_timeout'] = '7';

                $config['smtp_user'] = 'tworkreport@gmail.com';

                $config['smtp_pass'] = '8722248936';

                $config['charset'] = 'utf-8';

                $config['newline'] = "\r\n";

                $config['mailtype'] = 'html'; // or html

                $config['validation'] = TRUE; // bool whether to validate email or not      

                $this->email->initialize($config);
                $this->email->from('tworkreport@gmail.com', 'Terasukhintrade');
                $this->email->to($emailTo);


                $this->email->subject('Reset your password');

                $body=$this->load->view('forgotmailer',$data,TRUE);

                $this->email->message('Use the following OTP to reset password '.$body );

                $this->email->send();

                echo $this->email->print_debugger();

                $this->load->view('login',$data);
            } else {

                $this->session->set_flashdata('message', ' Email address not found!');
                $this->load->view('login',$data);
            }

        }
    }

2 个答案:

答案 0 :(得分:2)

使用ajax时,您总是需要返回值。您无法使用$this->load->view('login');

也在ajax函数中需要改变,如下所示。

        $.ajax({
            type: "POST",
            url: "<?php echo base_url() ?>" + "index.php/Login_cntrl/sendmail",
            data: {'email': email},
            dataType: 'json',
            success: function(data) {
                alert('Successfully send a mail');
            },
            error: function() {
                alert('fail');
            }
        });

答案 1 :(得分:-1)

public function sendmail(){

    $this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
    if ($this->form_validation->run() == FALSE) {
        $this->load->view('login');
    } else {

        $data['email'] = $this->input->post('email');

        $findemaill = $this->Login_model->getUserInfoByEmail($data['email']);



        if (!empty($findemaill)) {
           // $this->Login_model->getUserInfo('$id');
           // $this->session->set_userdata('forgot_password_flag', 'yes');
            $emailTo = $this->input->post('email');
               $this->session->set_userdata('findemaill',  $emailTo);
              //  $this->session->userdata('findemaill');

            $data['result'] = $this->Login_model->forgot_pass_retrive($emailTo);
            $id = $data['result']->id;
            $this->session->set_userdata('idd', $id);



            $six_digit_random_number = mt_rand(100000, 999999);

            $this->session->set_userdata('otp', $six_digit_random_number);

            $this->load->model('Login_model');

            $pass['pass'] = $this->Login_model->forgot_pass_retrive($emailTo);

            $config['protocol'] = 'smtp';

            $config['smtp_host'] = 'ssl://smtp.gmail.com';

            $config['smtp_port'] = '465';

            $config['smtp_timeout'] = '7';

            $config['smtp_user'] = 'tworkreport@gmail.com';

            $config['smtp_pass'] = '8722248936';

            $config['charset'] = 'utf-8';

            $config['newline'] = "\r\n";

            $config['mailtype'] = 'html'; // or html

            $config['validation'] = TRUE; // bool whether to validate email or not      

            $this->email->initialize($config);
            $this->email->from('tworkreport@gmail.com', 'Terasukhintrade');
            $this->email->to($emailTo);


            $this->email->subject('Reset your password');

            $body=$this->load->view('forgotmailer',$data,TRUE);

            $this->email->message('Use the following OTP to reset password '.$body );

            $this->email->send();

            echo $this->email->print_debugger();

            $data['is_data'] = '1';
            $data['msg'] = 'Successfully send a mail';
            echo json_encode($data);
        } else {

            $data['is_data'] = '0';
            $data['msg'] = 'Email address not found!';
            echo json_encode($data);

        }

    }
}