我想要返回一個值從控制器返回到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/>";
設置一個'flash_data'不會幫助你實現你的目標。您必須通過'ajax'從控制器回傳。從PHP回顯後,你可以在'ajax success'內使用'alert(result)'測試這個值。 – Arun
成功沒問題。如果電子郵件沒有發送,該怎麼辦?你能否給我一個這方面的片段。這將是非常有用的 – Jeeva
要使用flashdata,你應該使用'redirect()' –