使用Codeigniter和Bootstrap输入表单3 [英] Input Form Using Codeigniter and Bootstrap 3

查看:158
本文介绍了使用Codeigniter和Bootstrap输入表单3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我无法设计由PHP / Form Helper构建的文本字段,因为我不确定在哪里使用标签,我尝试过的每个解决方案都会导致出现额外的文本字段,或者只是出现插件,或者根本没有任何内容。

  public function __construct()
{
parent ::__构造();


public function index()
{
//获取所有用户
$ this-> data ['users'] = $ this - > user_m->得到();

//载入视图
$ this-> data ['subview'] ='admin / user / index';
$ this-> load-> view('admin / _layout_main',$ this-> data);


public function edit($ id = NULL)
{
//获取用户或设置新用户
if($ id) {
$ this-> data ['user'] = $ this-> user_m-> get($ id);
count($ this-> data ['user'])|| $ this-> data ['errors'] [] ='无法找到用户';
}
else {
$ this-> data ['user'] = $ this-> user_m-> get_new();
}

//设置表单
$ rules = $ this-> user_m-> rules_admin;
$ id || $ rules ['password'] ['rules']。='| required';
$ this-> form_validation-> set_rules($ rules);

//处理表单
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);
重定向('admin / user');
}

//载入视图
$ this-> data ['subview'] ='admin / user / edit';
$ this-> load-> view('admin / _layout_main',$ this-> data);


public function delete($ id)
{
$ this-> user_m-> delete($ id);
重定向('admin / user');


public function login()
{
//如果用户已经登录,重定向用户
$ dashboard ='admin / dashboard';
$ this-> user_m-> loggedin()== FALSE ||重定向($仪表盘);

//设置表格
$ rules = $ this-> user_m->规则;
$ this-> form_validation-> set_rules($ rules);

//处理表单
if($ this-> form_validation-> run()== TRUE){
//我们可以登录并重定向
if($ this-> user_m-> login()== TRUE){
redirect($ dashboard);
}
else {
$ this-> session-> set_flashdata('error','该电子邮件/密码组合不存在');
重定向('admin / user / login','refresh');


$ b $ //加载视图
$ this-> data ['subview'] ='admin / user / login';
$ this-> load-> view('admin / _layout_modal',$ this-> data);


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

公共函数_unique_email($ str)
{
//不验证电子邮件是否已经存在
//除非是电子邮件当前用户

$ id = $ this-> uri->段(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');
返回FALSE;
}

返回TRUE;
}
}

查看

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

< div class =container>
< div class =modal-header>
< h3>登入< / h3>
< p>请使用您的凭证登入< / p>
< / div>
< table class =table>
< tr>
< td>电子邮件< / td>
< td><?php echo form_input('email'); ?>< / TD>
< / tr>
< tr>
< td>密码< / 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回声日期('Y'); ?> <?php echo $ meta_title; ?>
< / div>
<?php echo form_close();?>

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



Bootstrap

 < form class =form-signinrole =form> 
< h2 class =form-signin-heading>请登入< / h2>
< input type =textclass =form-controlplaceholder =emailrequired autofocus value =<?php echo set_value('email')?>>
< input type =passwordclass =form-controlplaceholder =passwordrequired value<?php echo set_value('password')?>>
< label class =checkbox>
< input type =checkboxvalue =记住我>记住我
< / label>
< button class =btn btn-lg btn-primary btn-blocktype =submit>登入< / 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>请登入< / 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 '=>'密码']); ?>

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

< button class =btn btn-lg btn-primary btn-blocktype =submit>登入< / button>

<?php echo form_close(); ?>


I'm using Codeigniter, styled with Bootstrap 3 to build a website.

I can't stylize the text-fields built by the PHP/Form Helper, as I'm not sure where to use the tags, every solution I've tried has resulted in either an extra text field, or just the addon appearing, or nothing at all.

Controller

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;
  }
}

View

    <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>

Bootstrap

      <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>

解决方案

You just pass everything as an array to form_input

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

Here's your entire form as presented,

<?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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆