CodeIgniter表单未提交

时间:2019-02-16 15:29:08

标签: php codeigniter

我是php的新手,我正尝试使用CodeIgniter php框架开发一个简单的项目。我正在关注CodeIgniter网站上的教程。

我的问题是我已经创建了模型,视图和控制器作为教程。但是,当我单击“提交”按钮时,我的create表单没有提交。其他功能正常工作。

我遵循了以下教程: Tutorial

谢谢。

查看

<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
    <label for="title">Title</label>
    <input type="input" name="title" /><br />
    <label for="text">Text</label>
    <textarea name="text"></textarea><br />
    <input type="submit" name="submit" value="Create news item" />
</form>

型号

<?php

class News_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_news($slug = FALSE) {
        if ($slug === FALSE) {
            $query = $this->db->get('news');
            return $query->result_array();
        }

        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
    }

    public function set_news() {

        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);
    }

}

控制器

<?php

class News extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url_helper');
    }

    public function index() {
        $data['news'] = $this->news_model->get_news();

        $data['title'] = 'Title passes from conttoller';

        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }

    public function view($slug = NULL) {
        $data['news_item'] = $this->news_model->get_news($slug);

        if (empty($data['news_item'])) {
            show_404();
        }

        $data['title'] = $data['news_item']['title'];

        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
    }

    public function create() {

        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('news/create');
            $this->load->view('templates/footer');
        } else {
            $this->news_model->set_news();
            $this->load->view('news/success');
        }
    }

}


路线:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

1 个答案:

答案 0 :(得分:0)

如果您是Codeigniter的新手,请不要玩“ config / routes.php”

单击提交按钮时是否未提交

-moz-fit-content表单? HTML表单应默认提交表单(浏览器将刷新)

请先在浏览器中检查“检查元素>网络”,然后再单击表单,查看发生了什么情况