简体   繁体   English

如何在Codeigniter中获取表单输入框的值

[英]how to get the value of form input box in codeigniter

value of FORM INPUT Help!! FORM INPUT帮助的价值!!

//this is just a refrence of $nm and $fid from test_model// //这只是来自test_model的$ nm和$ fid的引用//

  $data['fid']['value'] = 0;
  $data['nm'] = array('name'=>'fname',
                      'id'=>'id');

say i have one form_view with 说我有一个form_view与

<?=form_label('Insert Your Name :')?>
<?=form_input($nm)?>

and a function to get single row 和获取单行的函数

 function get($id){
    $query = $this->db->getwhere('test',array('id'=>$id));
    return $query->row_array();
}

then in controller.. index($id = 0) 然后在控制器中.. index($ id = 0)

and somewhere in index 在索引中的某处

 if((int)$id > 0)
        {
            $q = $this->test_model->get($id);
            $data['fid']['value'] = $q['id'];
            $data['nm']['value'] = $q['name'];
        }

and mysql table has something like 1. victor, 2. visible etc. as a name value 和mysql表的名称值类似1. victor,2。visible等。

but here its not taking the value of name and id from form_input and not showing it again in form_view in same input box as victor etc so to update and post it back to database... 但是这里它没有从form_input中获取name和id的值,并且没有在与victor等的相同输入框中的form_view中再次显示它,因此进行更新并将其发布回数据库...

anyone please help!! 任何人请帮忙! and please be easy as I am new to CI!! 并请轻松,因为我是CI新手!!

Based on your comment to my first answer, here is a sample of a Controller, Model and View to update a user entry pulled from a table in a database. 根据您对我的第一个答案的评论,这是一个Controller,Model和View的示例,用于更新从数据库表中提取的用户条目。

Controller 控制者

class Users extends Controller
{
    function Users()
    {
        parent::Controller();
    }

    function browse()
    {
    }

    function edit($id)
    {
        // Fetch user by id
        $user = $this->user_model->get_user($id);

        // Form validation
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Name', 'required');

        if ($this->form_validation->run())
        {
            // Update user
            $user['name'] = $this->input->post('name', true);
            $this->user_model->update_user($user);

            // Redirect to some other page
            redirect('users/browse');
        }
        else
        {
            // Load edit view
            $this->load->view('users/edit', array('user' => $user));
        }
    }        
}

Model 模型

class User_model extends Model
{
    function User_model()
    {
        parent::Model();
    }

    function get_user($user_id)
    {
        $sql = 'select * from users where user_id=?';
        $query = $this->db->query($sql, array($user_id));
        return $query->row();
    }

    function update_user($user)
    {
        $this->db->where(array('user_id' => $user['user_id']));
        $this->db->update('users', $user);
    }
}

View 视图

<?php echo form_open('users/edit/' . $user['user_id']); ?>
<div>
    <label for="name">Name:</label>
    <input type="text" name="name" value="<?php echo set_value('name', $user['name']); ?>" />
</div>
<div>
    <input type="submit" value="Update" />
</div>
<?php echo form_close(); ?>

It's hard to see the problem from your snippets of code, please try and give a little more information as to the structure of your app and where these code samples are placed. 从您的代码片段很难看到问题,请尝试提供一些有关应用程序结构以及这些代码示例放置位置的信息。

Presume in the last code listing ('somewhere in index') you are getting $id from the form, but you define the ID of the form input box as 'id' array('name'=>'fname','id'=>'id') rather than an integer value so maybe this is where the problem lies. 假定在最后一个代码清单(“索引中的某处”)中,您将从表单中获取$ id,但是将表单输入框的ID定义为“ id” array('name'=>'fname','id' =>'id')而不是整数值,因此问题可能出在这里。

Where does the $data array get passed to in the third code listing? $ data数组在第三个代码清单中传递到哪里?

From your question I think you want to display a form to edit a person record in the database. 根据您的问题,我认为您想显示一个表单来编辑数据库中的人员记录。

Controller code 控制器代码

// Normally data object is retrieved from the database
// This is just to simplify the code
$person = array('id' => 1, 'name' => 'stephenc');

// Pass to the view
$this->load->view('my_view_name', array('person' => $person));

View code 查看代码

<?php echo form_label('Your name: ', 'name'); ?>
<?php echo form_input(array('name' => 'name', 'value' => $person['name'])); ?>

Don't forget to echo what is returned from form_label and form_input. 不要忘记回显form_label和form_input返回的内容。 This could be where you are going wrong. 这可能是您出问题的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM