输入表格使用Codeigniter和Bootstrap 3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了输入表格使用Codeigniter和Bootstrap 3相关的知识,希望对你有一定的参考价值。

我正在使用Codeigniter,使用Bootstrap 3来构建一个网站。

我无法对php / Form Helper构建的文本字段进行样式化,因为我不确定在哪里使用标记,我尝试过的每个解决方案都会导致额外的文本字段,或者只是出现插件,或者什么也没有。

调节器

public function __construct ()
{
    parent::__construct();
}

public function index ()
{
    // Fetch all users
    $this->data['users'] = $this->user_m->get();

    // Load view
    $this->data['subview'] = 'admin/user/index';
    $this->load->view('admin/_layout_main', $this->data);
}

public function edit ($id = NULL)
{
    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->user_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->user_m->get_new();
    }

    // Set up the form
    $rules = $this->user_m->rules_admin;
    $id || $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);

    // Process the form
    if ($this->form_validation->run() == TRUE) {
        $data = $this->user_m->array_from_post(array('name', 'email', 'password'));
        $data['password'] = $this->user_m->hash($data['password']);
        $this->user_m->save($data, $id);
        redirect('admin/user');
    }

    // Load the view
    $this->data['subview'] = 'admin/user/edit';
    $this->load->view('admin/_layout_main', $this->data);
}

public function delete ($id)
{
    $this->user_m->delete($id);
    redirect('admin/user');
}

public function login ()
{
    // Redirect a user if he's already logged in
    $dashboard = 'admin/dashboard';
    $this->user_m->loggedin() == FALSE || redirect($dashboard);

    // Set form
    $rules = $this->user_m->rules;
    $this->form_validation->set_rules($rules);

    // Process form
    if ($this->form_validation->run() == TRUE) {
        // We can login and redirect
        if ($this->user_m->login() == TRUE) {
            redirect($dashboard);
        }
        else {
            $this->session->set_flashdata('error', 'That email/password combination does not exist');
            redirect('admin/user/login', 'refresh');
        }
    }

    // Load view
    $this->data['subview'] = 'admin/user/login';
    $this->load->view('admin/_layout_modal', $this->data);
}

public function logout ()
{
    $this->user_m->logout();
    redirect('admin/user/login');
}

public function _unique_email ($str)
{
    // Do NOT validate if email already exists
    // UNLESS it's the email for the current user

    $id = $this->uri->segment(4);
    $this->db->where('email', $this->input->post('email'));
    !$id || $this->db->where('id !=', $id);
    $user = $this->user_m->get();

    if (count($user)) {
        $this->form_validation->set_message('_unique_email', '%s should be unique');
        return FALSE;
    }

    return TRUE;
  }
}

视图

    <div class="modal-body">
<?php echo validation_errors(); ?>
<?php echo form_open();?>

<div class="container">
    <div class="modal-header">
    <h3>Log in</h3>
    <p>Please log in using your credentials</p>
</div>
<table class="table">
    <tr>
        <td>Email</td>
        <td><?php echo form_input('email'); ?></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><?php echo form_password('password'); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Log in', 'class="btn btn-primary"'); ?></td>
    </tr>
</table>
        <div class="modal-footer">
            &copy; <?php echo date('Y'); ?> <?php echo $meta_title; ?>
        </div>
<?php echo form_close();?>

    </div>
</div>
</div>

引导

      <form class="form-signin" role="form">
    <h2 class="form-signin-heading">Please sign in</h2>
    <input type="text" class="form-control" placeholder="email" required autofocus value="<?php echo set_value('email') ?>">
    <input type="password" class="form-control" placeholder="password" required value"<?php echo set_value('password') ?>">
    <label class="checkbox">
      <input type="checkbox" value="remember-me"> Remember me
    </label>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  </form>
答案

您只需将数组作为数组传递给form_input

<?php echo form_input(['name' => 'email', 'id' => 'email', 'class' => 'form-control', 'value' => set_value('email')]); ?>

这是您提供的整个表格,

<?php echo form_open('controller/method', ['class' => 'form-signin', 'role' => 'form']); ?>

<h2 class="form-signin-heading">Please sign in</h2>

<?php echo form_input(['name' => 'email', 'id' => 'email', 'class' => 'form-control', 'value' => set_value('email'), 'placeholder' => 'Email']); ?>

<?php echo form_password(['name' => 'password', 'id' => 'password', 'class' => 'form-control', 'placeholder' => 'Password']); ?>

<label class="checkbox">
    <?php echo form_checkbox(['name' => 'remember_me', 'value' => 1]); ?>
</label>

<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>

<?php echo form_close(); ?>

以上是关于输入表格使用Codeigniter和Bootstrap 3的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Codeigniter 3 一次将图像输入到 2 个表中

如何修复我的表单没有将数据更新到数据库 [Codeigniter]

CodeIgniter POST调用输出状态:303参见其他

Codeigniter 在表单中验证数组

Codeigniter-检查用户是不是存在于月份

Codeigniter 3:不要在表单验证错误时重新加载表单