制作一个简单的 codeigniter 3 Web 应用程序 我想通过表单将类别名称添加到数据库,但即使在提交后,设置的 flashdata 消息也没有显示在前端..尽管我可以看到数据添加到 phpmyadmin 中的表中
这是我的代码视图list.php
<?php $this->load->view('admin/header'); ?>
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Categories</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active">Categories</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<?php
if($this->session->flashdata('success') != ""){ ?>
<div class='alert alert-success mb-3'><?php echo $this->session->flashdata('success');?></div>;
<?php }
?>
<!--<div class="alert alert-success">success</div>-->
<div class="card">
<!--<div class="card-body d-flex justify-content-center align-items-center" style="height:450px;">
<h3 class="">Welcome to Codeigniter Web Console</h5>
</div>-->
<div class="card-header">
<div class="card-title">
<form id="SearchFrm" name="searchFrm" action="" method="">
<div class="input-group mb-0">
<input type="text" value="" class="form-control" placeholder="Search" id="" name="q">
<div class="input-group-append">
<button class="input-group-text"><i class="fas fa-search"></i></button>
</div>
</div>
</form>
</div><!--card-title-->
<div class="card-tools">
<a href="<?php echo base_url().'admin/category/create' ?>" class="btn btn-primary"><i class="fas fa-plus"></i> Create </a>
</div>
</div><!--card-header-->
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th width="50">#</th>
<th>Name</th>
<th width="100">Status</th>
<th width="160" class="text-center">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Fashion</td>
<td><span class="badge badge-success">Staus</span></td>
<td class="text-center"><a href="" class="btn btn-sm btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
</tr>
<tr>
<td>1</td>
<td>Fashion</td>
<td><span class="badge badge-success">Staus</span></td>
<td class="text-center"><a href="" class="btn btn-sm btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
</tr>
<tr>
<td>1</td>
<td>Fashion</td>
<td><span class="badge badge-success">Staus</span></td>
<td class="text-center"><a href="" class="btn btn-sm btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
</tr>
</tbody>
</table>
</div><!--card-body-->
</div>
</div>
<!-- /.col-md-6 -->
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php $this->load->view('admin/footer'); ?>
我什至尝试过这个代码
<?php if($this->session->flashdata('success') != ""){ ?>
<div class="alert alert-success"><?php echo $this->session->flashdata('success'); ?></div>
<?php } ?>
但这也不起作用
更新 正如 dontpanic 所说,我已经更新了我的代码 类别.php(控制器)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Category extends CI_Controller {
//this method will show category list page
public function index()
{
$this->load->view('admin/category/list');
}
//this method will show create category page
public function create()
{
$this->load->model('Category_model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters(' <p class="text-danger">','</p>');
$this->form_validation->set_rules('name','Name','trim|required');
if($this->form_validation->run() == true){
//will check category in database
$formArray['name'] = $this->input->post('name');
$formArray['status'] = $this->input->post('status');
$formArray['created_at'] = date('Y-m-d H:i:s');
$this->Category_model->create($formArray);
$this->session->flashdata('success','Category added successfully');
redirect (base_url().'admin/category/index');
}else{
//Will show errors
$this->load->view('admin/category/create');
}
// $this->load->view('admin/category/create');
}
//this method will show edit category page
public function edit()
{
}
//this method will will delete a category
public function delete()
{
}
}
Category_model.php(模型)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Category_model extends CI_Model {
public function create($formArray){
$this->db->insert('categories',$formArray);
}
}
并更新了 list.php(根据 vansh patel 的提示)(视图)
<?php $this->load->view('admin/header'); ?>
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Categories</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active">Categories</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<?php
$flashdata = $this->session->flashdata('success');
if($flashdata != ""){ ?>
<div class='alert alert-success mb-3'><?php echo $this->session->flashdata('success');?></div>;
<?php }
?>
<!--<div class="alert alert-success">success</div>-->
<div class="card">
<!--<div class="card-body d-flex justify-content-center align-items-center" style="height:450px;">
<h3 class="">Welcome to Codeigniter Web Console</h5>
</div>-->
<div class="card-header">
<div class="card-title">
<form id="SearchFrm" name="searchFrm" action="" method="">
<div class="input-group mb-0">
<input type="text" value="" class="form-control" placeholder="Search" id="" name="q">
<div class="input-group-append">
<button class="input-group-text"><i class="fas fa-search"></i></button>
</div>
</div>
</form>
</div><!--card-title-->
<div class="card-tools">
<a href="<?php echo base_url().'admin/category/create' ?>" class="btn btn-primary"><i class="fas fa-plus"></i> Create </a>
</div>
</div><!--card-header-->
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th width="50">#</th>
<th>Name</th>
<th width="100">Status</th>
<th width="160" class="text-center">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Fashion</td>
<td><span class="badge badge-success">Staus</span></td>
<td class="text-center"><a href="" class="btn btn-sm btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
</tr>
<tr>
<td>1</td>
<td>Fashion</td>
<td><span class="badge badge-success">Staus</span></td>
<td class="text-center"><a href="" class="btn btn-sm btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
</tr>
<tr>
<td>1</td>
<td>Fashion</td>
<td><span class="badge badge-success">Staus</span></td>
<td class="text-center"><a href="" class="btn btn-sm btn-primary"><i class="fas fa-edit"></i> Edit</a><a href="" class="btn btn-sm btn-danger ml-1"><i class="fas fa-trash-alt"></i> Delete</a></td>
</tr>
</tbody>
</table>
</div><!--card-body-->
</div>
</div>
<!-- /.col-md-6 -->
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php $this->load->view('admin/footer'); ?>
create.php(视图)(其中存在用于添加类别的表单)
<?php $this->load->view('admin/header'); ?>
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Categories</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item"><a href="<?php echo base_url().'admin/category/index' ?>">Categories</a></li>
<li class="breadcrumb-item active">Create New Category</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="card card-primary">
<!--<div class="card-body d-flex justify-content-center align-items-center" style="height:450px;">
<h3 class="">Welcome to Codeigniter Web Console</h5>
</div>-->
<div class="card-header">
<div class="card-title">
Create New Category
</div><!--card-title-->
</div><!--card-header-->
<form name="categoryForm" id="categoryForm" method="post" action="<?php echo base_url().'admin/category/create' ?>">
<div class="card-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control <?php echo( form_error('name') != "") ? 'is-invalid' : ''; ?>" value="" name="name" id="name">
<?php echo form_error('name');?>
</div>
<div class="form-group">
<label>Image</label>
<input type="file" class="form-control-file border" name="image" id="image">
</div>
<div class="form-group row">
<div class="col-sm-1 col-xs-1">
<div class="custom-control custom-radio">
<input class="custom-control-input" type="radio" value="1" id="statusActive" name="status" checked>
<label for="statusActive" class="custom-control-label">Active</label>
</div>
</div>
<div class="col-sm-1 col-xs-1">
<div class="custom-control custom-radio ml-3">
<input class="custom-control-input" type="radio" value="0" id="statusBlock" name="status">
<label for="statusBlock" class="custom-control-label">Block</label>
</div>
</div>
</div>
<!--<div class="custom-control custom-radio float-left">
<input class="custom-control-input" type="radio" value="1" id="statusActive" name="statusActive" checked>
<label for="statusActive" class="custom-control-label">Active</label>
</div>
<div class="custom-control custom-radio ml-3">
<input class="custom-control-input" type="radio" value="1" id="statusBlock" name="statusBlock">
<label for="statusBlock" class="custom-control-label">Block</label>
</div>-->
</div><!--card-body-->
<div class="card-footer">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
<a href="<?php echo base_url().'admin/category/index' ?>" class="btn btn-secondary">Back</a>
</div><!--card-footer-->
</form>
</div>
</div>
<!-- /.col-md-6 -->
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php $this->load->view('admin/footer'); ?>
if 条件 将 flashdata 消息操作验证为 true,并且在重定向后将在 view 中可见,因此代码将如下
<?php if($this->session->flashdata('success')): ?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>