在Codeigniter 404中按下提交按钮后出现错误

时间:2017-12-28 17:12:23

标签: php html codeigniter contact-form-7

我创建了一个联系表单,将详细信息提交到名为'ccp'的数据库,表名为'queries' 但是在按下提交按钮后,它不会指向任何页面,也不会向数据库提交数据。请帮我修复此错误。

控制器文件 - Contactform.php

<?php

class Contactform extends CI_Controller {

public function __construct(){

        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library(array('session','form_validation'));

        $this->load->database();

}
function index(){
        //set validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|callback_alpha_space_only');
        $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'trim|required');
        $this->form_validation->set_rules('message', 'Message', 'trim|required');

        //run validation on post data
        if ($this->form_validation->run() == FALSE)
        {   //validation fails
            $this->load->view('Contact_form_view');
        }
        else
        {
            //insert the contact form data into database
            $data = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'subject' => $this->input->post('subject'),
                'message' => $this->input->post('message')
            );

            if ($this->db->insert('inquiries', $data))
            {
                // success
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
                redirect('Contactform/index');
            }
            else
            {
                // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error.  Please try again later!!!</div>');
                redirect('Contactform/index');
            }
        }
}
    //custom callback to accept only alphabets and space input
function alpha_space_only($str){
        if (!preg_match("/^[a-zA-Z ]+$/",$str))
        {
            $this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
}
}
?>

查看文件 - Contact_form_view.php

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ciao Code Contact Form</title>
    <link href="<?php echo base_url("css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="col-md-6 col-md-offset-3">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Contact Form</h3>
        </div>
        <div class="panel-body">
            <?php $attributes = array("name" => "Contactform");
            echo form_open("Contactform/index", $attributes);?>
            <div class="form-group">
                <label for="name">Name</label>
                <input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
                <span class="text-danger"><?php echo form_error('name'); ?></span>
            </div>

            <div class="form-group">
                <label for="email">Email ID</label>
                <input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
                <span class="text-danger"><?php echo form_error('email'); ?></span>
            </div>

            <div class="form-group">
                <label for="subject">Subject</label>
                <input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
                <span class="text-danger"><?php echo form_error('subject'); ?></span>
            </div>

            <div class="form-group">
                <label for="message">Message</label>
                <textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
                <span class="text-danger"><?php echo form_error('message'); ?></span>
            </div>

            <div class="form-group">
                <button name="submit" type="submit" class="btn btn-success">Submit</button>
            </div>
            <?php echo form_close(); ?>
            <?php echo $this->session->flashdata('msg'); ?>
        </div>
    </div>
</div>
</body>
</html>

除此之外,database.php文件更新如下

    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'ccp',
    'dbdriver' => 'mysqli',

和routes.php如下:

$route['default_controller'] = 'Contactform';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

以下是我得到的错误 enter image description here

1 个答案:

答案 0 :(得分:2)

遵循这些方法

  1. 设置项目的基本URL。 Check my answer to another question
    • 在图片中,我可以看到[::1]之类的内容。因为每当基本URL为空时它都会被克服。
  2. 在没有index.php的情况下加载网站。Check .htaccess mentioned in another question

    • 您可以在表单中添加index.php以避免这种情况。但不推荐。

      echo form_open("index.php/Contactform/index", $attributes);?>
      
相关问题