如果邮件发送成功或错误Codeigniter AJAX如何返回
我想要返回一个值从控制器返回到ajax发送电子邮件的时间和地点。以下是一些代码。如果邮件发送成功或错误Codeigniter AJAX如何返回
在下面的代码,我想设置在会话中的闪存数据,并应设置的任一条件,并呼吁它回到了AJAX
控制器
public function send_reply(){ $data = array(
'from_email' => "[email protected]",
'recipient' => $this->input->post('recipient'),
'from_name' => "jeeva",
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message'),
);
echo json_encode($data);
$this->load->library('email');
$this->email->from($data['from_email'], $data['from_name']);
$this->email->to($data['recipient']);
$this->email->subject($data['subject']);
$this->email->message($data['message']);
//Send mail
if($this->email->send()) {
$this->session->set_flashdata("email_sent","<span> * Email sent successfully. </span> ");
//$this->load->view('adminhp');
} else {
$this->session->set_flashdata("email_sent","<span> * Error in sending Email.</span> ");
// $this->load->view('adminhp');
}
}
Ajax代码
// Ajax post for email sending $(document).ready( function() {
$(".mailsub").click(function(event) {
event.preventDefault();
var recipient = jQuery(this).closest('form').find('input.recipient').val();
var subject = jQuery(this).closest('form').find('input.subject').val();
var message = jQuery(this).closest('form').find('textarea.message').val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/user_authentication/send_reply",
dataType: 'json',
data: {recipient: recipient, subject: subject, message:message},
success: function(result) {
if (result)
{
// Show Entered Value
jQuery("div#result").show();
jQuery("div#rslt").html("Sent Successfully");
console.log("Success");
}
}
});
});
});
我想将它设置为fla在下面的代码SH数据
echo "<br/>"; echo "<div id='result' style='display: none'>";
echo "<div id='rslt' style='display:inline'></div>";
// echo $this->session->flashdata('email_sent');
echo "</div>";
echo "<br/>";
回答:
首先在控制器,你必须呼应消息阿贾克斯
$msg = ''; if($this->email->send()) {
$msg = 'Email sent successfully';
} else {
$msg ='Error in sending';
}
echo $msg;die;
第二只输入反应添加到相应的div:
success: function(result) { if (result)
{
console.log(result);
jQuery("div#result").show();
jQuery("div#rslt").html(result);
}
以上是 如果邮件发送成功或错误Codeigniter AJAX如何返回 的全部内容, 来源链接: utcz.com/qa/257813.html