在codeigniter中的表中插入表单值

时间:2018-05-18 09:14:15

标签: php codeigniter

我正在尝试在database.i中插入值。我不知道我的表单值未成功插入的问题是什么。当我点击创建用户按钮时,我的表单将在模态弹出窗口中打开。

我不知道为什么我的表单验证也无法正常工作。

这是我的控制器:

  public function createOrgUsers() {

    //Validating Name Field
    $this->form_validation->set_rules('username', 'username', 'required');
    $this->form_validation->set_rules('firstname', 'firstname', 'required');
    $this->form_validation->set_rules('lastname', 'lastname', 'required');
    $this->form_validation->set_rules('email', 'email', 'required');
    $this->form_validation->set_rules('password', 'password', 'required');




    if ($this->form_validation->run() == FALSE) {
        redirect('Organisation/view_organisations');
    } else {
        //Setting values for tabel columns
        $data = array(
            'username' => $this->input->post('username'),
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname'),
            'email' => $this->input->post('email'),
            'password' => $this->input->post('password')
        );

        //Transfering data to Model
        $this->Org_model->orgUsers($data);
        $data['message'] = 'Data Inserted Successfully';
        //Loading View
        redirect('Organisation/editOrg');
    }
}

这是我的模特:

function orgUsers($data) {
    // Inserting in Table(users)
    $this->db->insert('users', $data);
}

这是我的视图弹出窗口:

<div class="modal fade" id="createUser" class="modal" tabindex="-1" role="dialog" aria-labelledby="createUser" aria-hidden="true">
                <div class="modal-dialog">
                        <div class="modal-content" id="modal-content">
                            <form class="form-horizontal" id="admin-create-user" action="<?php echo base_url(); ?>Organisation/createOrgUsers" method="post" role="form">

                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">Create New User</h4>
                                </div>
                                <div class="modal-body">

                                    <div class="form-group">
                                        <label for="inputUsername" class="col-sm-4 control-label">Username:</label>
                                        <div class="col-sm-7">
                                            <input name="username" type="text" class="form-control" id="inputUsername" placeholder="Username" >                            
                                        </div>
                                        <div class="col-sm-4">
                                            <small></small>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label for="inputFirstname" class="col-sm-4 control-label">First Name:</label>
                                        <div class="col-sm-7">
                                            <input type="text" name="firstname" class="form-control" id="inputFirstname" placeholder="First Name" >                             
                                        </div>
                                        <div class="col-sm-4">
                                            <small></small>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label for="inputLastname" class="col-sm-4 control-label">Last Name:</label>
                                        <div class="col-sm-7">
                                            <input type="text" name="lastname" class="form-control" id="inputLastname" placeholder="Last Name" >
                                        </div>
                                        <div class="col-sm-4">
                                            <small></small>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label for="email" class="col-sm-4 control-label">E-mail:</label>
                                        <div class="col-sm-7">
                                            <input type="text" id="email" name="email" class="form-control" placeholder="Email" >
                                        </div>
                                        <div class="col-sm-4">
                                            <small></small>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label for="inputPassword" class="col-sm-4 control-label">Password:</label>
                                        <div class="col-sm-7">
                                            <input type="password" name="password" class="form-control" id="inputPassword" placeholder="Password">
                                        </div>
                                        <div class="col-sm-4">
                                            <small></small>
                                        </div>
                                    </div>


                                <input type="hidden" name="form_submission" value="admin_registration">                                         

                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    <button type="submit" value="submit" class="btn btn-primary" id="submit3" >Create New User</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

任何人都可以在我错误的地方帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在您的聊天中,您想要显示按钮,请单击模式弹出窗口并按其id显示用户数据。

我的建议是使用Ajax

<a href='<?php echo base_url()?>"Organisation/editOrg?id=$org->id' title='Edit' class='open_modal btn btn-default' id="userView">
<i class='fas fa-edit'></i>
</a>

<div class="modal fade" id="ShowUser" class="modal" tabindex="-1" role="dialog" aria-labelledby="createUser" aria-hidden="true">
    <div class="modal-dialog">
            <div class="modal-content" id="modal-content">
                <form class="form-horizontal" id="admin-create-user" action="<?php echo base_url(); ?>Organisation/createOrgUsers" method="post" role="form">

                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Create New User</h4>
                    </div>
                    <div class="modal-body">

                        <div class="form-group">
                            <label for="inputUsername" class="col-sm-4 control-label">Username:</label>
                            <div class="col-sm-7">
                                <input name="username" type="text" class="form-control" id="inputUsername" placeholder="Username" value="">                            
                            </div>
                            <div class="col-sm-4">
                                <small></small>
                            </div>
                        </div>


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

AJAX:

$('#userView').click(function(){
var id = <?php echo $_GET['id'];
    $.ajax({
        url: "contoller_name",
        type: 'POST',
        data: "id=" + id,
        success: function(returndata){
              $('#ShowUser').modal('show');
              // to create id and pass the all data via id
              var  usename = returndata.username;

              $('#inputUsername').val(usename);
        }
    });
});

控制器:

    function contoller_name()
{
    $id = this->input->post('id');

    $data = $this->modal_name->modelFuncName($id);
    $this->output->set_content_type('application/json')
             ->set_output(json_encode($data));
}

型号:

function modelFuncName($id)
{
    $this->db->select('*');
    $this->db->from('table_name');
    $this->db->where('id', $id);
    $query = $this->db->get();
    return $query->result();
}