如何使用Codeigniter从Bootstrap Modal保存数据?

时间:2015-09-29 10:12:49

标签: php twitter-bootstrap codeigniter

我正在尝试保存从模态中插入的数据。现在我有一个名为users的视图,其中我显示了users.now如果管理员点击添加更多用户,它会打开一个bootstap模式,其中我有一个名为no的用户请求的字段。因此管理员填写的任何值必须保存到数据库中。我很担心如何保存它。

  

编辑.............................................. .....

在转销商登录后,他可以请求更多用户加入管理员,因此我创建了该模式以请求所需的用户,因此我希望存储该转销商所需的用户数量以及他/她的密钥

我的控制器是:

public function index ()
{
     $usertype=$this->session->userdata('usertype');
    if($usertype ==="reseller")
    {
    $key= $this->session->userdata('key');

    $this->db->where("key",$this->session->userdata('key'));        
    $this->data['users'] = $this->user_m->get();


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

}

}

我的观点是:

    <a href="#" class="btn btn-success" data-toggle="modal" data-target="#myModal">Request More Users</a>

    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Add the number of users you want</h4>
          </div>
          <div class="modal-body">
            <form id="loginForm" method="post" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-xs-3 control-label">Number Of Users</label>
                            <div class="col-xs-5">
                                <input type="text" class="form-control" name="username" id="spinnerInput" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-xs-3 control-label">Password</label>
                            <div class="col-xs-5">
                                <input type="password" class="form-control" name="password" />
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-xs-5 col-xs-offset-3">
                                <button type="submit" class="btn btn-primary">Submit</button>

                            </div>
                        </div>
                    </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>

      </div>
    </div>

1 个答案:

答案 0 :(得分:1)

你的问题不清楚,但会在之前给出。在您的表单上,如果您没有表单操作,则需要进行表单操作然后将无效。确保url helper也是自动加载的。

文档http://www.codeigniter.com/docs

还会查看codeigniter表单验证http://www.codeigniter.com/user_guide/libraries/form_validation.html

并形成帮手

http://www.codeigniter.com/user_guide/helpers/form_helper.html

<form action="<?php echo base_url('controller-name/function');?>" method="post" method="post">

您可能需要在base_url();

中使用index.php

<form action="<?php echo base_url('index.php/controller-name/function');?>">

查看

<a href="#" class="btn btn-success" data-toggle="modal" data-target="#myModal">Request More Users</a>

<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Add the number of users you want</h4>
</div>
<div class="modal-body">
<form id="loginForm" method="post" class="form-horizontal" action="<?php echo base_url('controller-name/request');?>">

<div class="form-group">
<label class="col-xs-3 control-label">Number Of Users</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="username" id="spinnerInput" />
</div>
</div>

<div class="form-group">
<label class="col-xs-3 control-label">Password</label>
<div class="col-xs-5">
<input type="password" class="form-control" name="password" />
</div>
</div>

<div class="form-group">
<div class="col-xs-5 col-xs-offset-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>

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

模型

class User_m extend CI_Model {

function request_user() {
    $data = array(
        'key' => $this->session->userdata('key'),
        'total_user' => $this->get_total(), // create a column in table
        'username' => $this->input->post('username')
    );

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

function request_update_user() {
    $data = array(
        'total_user' => $this->get_total() // create a column in table
        'username' => $this->input->post('username')
    );
    $this->db->where('key', $this->session->userdata('key'));
    $this->db->update('table', $data);
}

function get() {
    $this->db->where('key', $this->session->userdata('key'));
    $query = $this->db->get('table');
    return $query->result_array();
}

function get_total() {
   $this->db->where('key', $this->session->userdata('key'));
   $query = $this->db->get('table');
   return $query->num_rows();
}

}

控制器

class Reseller extends CI_Controller {

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

public function index () {

    $usertype = $this->session->userdata('usertype');

    if($usertype == "reseller") {

    $this->data['users'] = $this->user_m->get();

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

} else {

    $this->load->view('permission');
}

}

public function request() {
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'required');

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('modal_view');

    } else {
        $this->user_model->request_user();
        redirect('your_controller');
    }
}

}