Codeigniter联系表单提交返回500错误。

时间:2014-05-08 15:44:01

标签: javascript php jquery forms codeigniter

所以我正在尝试创建一个联系表单。

HTML / view:

            <form action="" class="horizontal-form" id="contact-form" method="post">
                <h3 class="form-section">Help Form</h3>
                <div class="form-group">
                    <label class="control-label">Name</label>
                    <input type="text" class="form-control col-md-12" name="name" id="name" />
                </div>
                <div class="form-group">
                    <label class="control-label" >Email</label>
                    <input type="text" class="form-control col-md-12" name="email" id="email">
                </div>
                <div class="form-group">
                    <label class="control-label" >Issues / Feedback</label>
                    <textarea class="form-control col-md-12" rows="3" name="message" id="message"></textarea>
                </div>
                <div class="margin-top-10">
                    <button type="submit" class="btn btn-info"><i class="icon-ok"></i> Send</button>
                    <button type="button" class="btn btn-default">Cancel</button>
                </div>
            </form>

javascript(在document.ready中):

            $('#contact-form').validate({
                rules: {
                    name: {
                        minlength: 2,
                        required: true
                    },
                    email: {
                        required: true,
                        email: true
                    },
                    message: {
                        minlength: 2,
                        required: true
                    }
                },
                highlight: function(element) {
                    $(element).closest('.control-group').removeClass('has-success').addClass('has-error');
                },
                success: function(element) {
                    element.text('OK!').addClass('valid').closest('.control-group').removeClass('has-error').addClass('has-success');
                },
                submitHandler: function(form) {
                    $.ajax({
                        url: 'contact_process',
                        data: $('#contact-form').serialize(),
                        type: "POST",
                        success: function(data) {
                            $("#contact-form").hide('slow');
                            $('#results').html(data);
                        }
                    });
                    return false;
                }
            });

支持控制器contact_process操作:

public function contact_process() {      
    $this->load->library('email');
    //grab the post data
    $this->email->from($this->input->post('email'), $this->input->post('name'));
    $this->email->to('bgarrison@cognigencorp.com');
    $this->email->subject('Cognigen Helpdesk Request');
    $this->email->message($this->input->post('message'));

    if ($this->email->send()) {
        echo "We have successfully received your email. We will Contact you ASAP.";
    } else {
        echo "Some problem occurred.";
    }
}

正如您所见,我的表单正在验证,然后提交到index.php / support / contact_process。但是当它出现时,我得到An Error Was Encountered The action you have requested is not allowed.和500服务器错误。当我手动转到网址时它工作正常。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试改变这个:

          submitHandler: function(form) {
                $.ajax({
                    url: 'contact_process',
                    data: $('#contact-form').serialize(),
                    type: "POST",
                    success: function(data) {
                        $("#contact-form").hide('slow');
                        $('#results').html(data);
                    }
                });
                return false;
            }

到此:

          var site_url ="<?php echo(site_url('/support/contact_process')); ?>";
          submitHandler: function(form) {
                $.ajax({
                    url: site_url,
                    data: $('#contact-form').serialize(),
                    type: "POST",
                    success: function(data) {
                        $("#contact-form").hide('slow');
                        $('#results').html(data);
                    }
                });
                return false;
            }