您好,我正在尝试使用 ajax 和 codeigniter 发送邮件。一切正常,但如果有人有想法,表格错误不会说话!
<script>
$(function() {
$('#mail').submit(function(e){
e.preventDefault();
var form_data = {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val()
};
$.ajax({
url : '<?php echo base_url('contact/send_email'); ?>',
data : form_data,
type : 'POST',
dataType : 'JSON',
success: function(result) {
if(result.valid == 1)
{
$('#mail').prepend('<div id="success" class="alert alert-success">Success</div>');
$('#success').fadeOut(5000);
}
else
{
$('#mail').prepend('<div id="error" class="alert alert-danger">Error</div>');
$('#error').fadeOut(5000);
}
}
})
});
});
</script>
这里是我的控制器:
public function send_email() {
$result = array();
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div id="error">', '</div>');
$this->form_validation->set_rules('name','Name','trim|required|xss_clean');
$this->form_validation->set_rules('email','Email Address','trim|required|xss_clean');
$this->form_validation->set_rules('message','Message','trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$result['valid'] = 0;
$data['success'] = '';
$this->lang->load('header');
//$this->lang->load('contact');
$this->lang->load('footer');
$this->load->view('includes/header', array('app_name' => 'contact', 'csrf_token_name' => $this->security->get_csrf_token_name(), 'csrf_hash' => $this->security->get_csrf_hash()));
$this->load->view('contact', $data);
$this->load->view('includes/footer');
}
else
{
$this->lang->load('header');
//$this->lang->load('contact');
$this->lang->load('footer');
$this->load->view('includes/header', array('app_name' => 'contact', 'csrf_token_name' => $this->security->get_csrf_token_name(), 'csrf_hash' => $this->security->get_csrf_hash()));
$this->load->view('contact', $data);
$this->load->view('includes/footer');
$result['valid'] = 1;
$data['success'] = 'Email envoyer avec succés';
$this->load->library('form_validation');
$this->load->library('email');
$this->email->from($this->input->post('email'), $this->input->post('name'));
$this->email->to('lauriaweb@gmail.com');
$this->email->subject('Message de PoP-Stream');
$this->email->message($this->input->post('message'));
$this->email->send();
}
die(json_encode($result));
}
}
我不明白如何犯错误,我迷路了^^
提前谢谢!