简体   繁体   English

使用ajax提交表单后无法显示成功消息

[英]unable to show success message after submitting a form using ajax

I am sending email using ajax after when ajax form is submitted. 提交ajax表单后,我正在使用ajax发送电子邮件。

Here is my php controller code: If i keep the email sending code inside this function then the SUCCESS message is not showing on the html page. 这是我的php控制器代码:如果我将电子邮件发送代码保留在此函数内,则SUCCESS消息不会显示在html页面上。 But if i remove the Email sending code from that function then SUCCESS message is showing. 但是,如果我从该功能中删除Email sending代码,则会显示SUCCESS消息。

public function  sendedits()
   {
     $this->load->library('form_validation');
     $this->form_validation->set_error_delimiters('<li  class="errorlist">', '</li>')->set_rules('menu_name', 'Title', 'trim|required|min_length[2]|max_length[255]|xss_clean');

        //user is logged in proceed the next work
           if (!$this->form_validation->run()) 
           {   //False  
               $this->_status['status'] = "error";
               $this->_status['message'] = $this->load->view('commonfiles/ajax_error_display_1', '', TRUE);
           } 

            else if ($this->form_validation->run() && $this->input->post('myId')=='')//myId=just for checking robot or human
             {      //TRUE block   
              $fname=$this->input->post('menu_name');
              $sendersemail=$this->input->post('changes_made');     
              $intrested_message=$this->input->post('content');   
               $config['protocol'] = 'smtp';
               $config['smtp_host'] = 'ssl://smtp.googlemail.com';
               $config['smtp_port'] = 465;
               $config['smtp_user'] = 'asdsdsd@gmail.com';
               $config['smtp_pass'] = 'sdsfsdfsdfsdfsds'; 
               $this->load->library('email', $config);
               $this->email->set_newline("\r\n"); 
               //$this->email->from($sendersemail, $fname);
               $this->email->to('ssdd@myemil.com');     
                    $this->email->subject('User Edited article of : '.$fname); 
                   $message = '<html>
                                       <head>
                                           <title></title>
                                           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                                       </head>
                                       <body>
                                        <table cellspacing="0" cellpadding="0" style="border-collapse:collapse;width:98%;background-color:whitesmoke" border="1">
                                            <tbody>
                                                <tr>
                                                       <td colspan=2><h2> '.$fname.'</h2></td>
                                                </tr>  
                                                <tr>
                                                     <td width=50%>First  Name </td>
                                                      <td width=50%>'.$fname.'</td>
                                               </tr>      
                                               <tr>
                                                     <td width=50%>Email </td>
                                                      <td width=50%>'.$sendersemail.'</td>
                                               </tr>     

                                                    <tr>
                                                     <td width=50%>Message </td>
                                                      <td width=50%>'.$intrested_message.'</td>
                                               </tr>   </tbody> <table> </body></html>  ';
               $this->email->message($message); 
               $this->email->send(); 
              $this->_status['message'] = 'Thankyou for your edits. We will review it before publishing.'; 
               $this->_status['status'] = "success"; 
             } 
          echo json_encode($this->_status);    
   } 

Ajax function for submitting the form 用于提交表单的Ajax函数

<script type='text/javascript'>
    $(document).ready(function() {

        var _status = $('#status');
        $('#sub2').click(function(e) {

            _status.html('');
            var postData = $('#form_id').serializeArray();
            var formURL = $('#form_id').attr("action");
            $.ajax({
                url: formURL,
                type: "POST",
                data: postData,
                dataType: "json",
                success: function(dat) {
                    if (dat.status === 'success') { 
                        _status.html('<span class="success">' + dat.message + '</span>');
                    }
                    else if (dat.status === 'fail') {

                    }
                    else
                    {

                        _status.html('<span class="err">' + dat.message + '</span>');

                    }
                },
                error: function(e) {
                    alert("Ooops! Try again later or else sends us  message regarding this issue. Thankyou!");
                }
            });

        });

    });

</script>

So Im guessing this is using the phpmailer library, do you have that installed (it might come as default depending on your platform) 因此,我猜测这是使用phpmailer库,您是否已安装该库(可能默认情况下取决于您的平台)

The first thing that I can see is that $sendersemail is not defined. 我能看到的第一件事是未定义$sendersemail Try inserting a string instead of the variable. 尝试插入字符串而不是变量。 The phpmailer library is terrible for cactching errors, I can never figure out how to tell where the errors are either! phpmailer库非常容易发生错误,我也永远无法弄清楚如何判断错误在哪里!

If that doesnt work, try outputting to the console after the line you think is giving you the error. 如果那不起作用,请尝试在您认为给您错误的那行之后输出到控制台。 If you use: 如果您使用:

echo("email sent"); echo(“电子邮件已发送”);

Then you should see it in the browser console as the code is being processed. 然后,在处理代码时,您应该在浏览器控制台中看到它。 Hopefully you can see where the error is coming from then. 希望您能看到错误从何而来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM