0

您好,我正在尝试使用 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));
        }
    }

我不明白如何犯错误,我迷路了^^

提前谢谢!

4

1 回答 1

0

您只是在代码中标记分隔符

$this->form_validation->set_error_delimiters('<div id="error">', '</div>');

这不会显示错误,而只会用 id 为“error”的 div 包装它们。要显示它们,您必须将以下内容添加到您的视图中

<?php echo validation_errors(); ?>

下面提到了可以实现相同目的的其他方法。

<?php echo validation_errors('<div class="error">', '</div>'); ?>

否则您将无法看到验证错误。您还可以将单个错误显示为

<?php echo form_error('field name', '<div class="error">', '</div>'); ?>

在这里阅读更多

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

于 2013-09-13T14:33:38.047 回答