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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2302362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:56:18  来源:igfitidea点击:

how to get the value of form input box in codeigniter

phpmysqlformscodeigniterinput

提问by Sam

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 的新手!!

采纳答案by Stephen Curran

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

控制器

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(); ?>

回答by Hymanbot

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.

假设在最后一个代码清单('somewhere in index')中,您从表单中获取 $id,但是您将表单输入框的 ID 定义为 'id' array('name'=>'fname','id' =>'id') 而不是整数值,所以也许这就是问题所在。

Where does the $data array get passed to in the third code listing?

$data 数组在第三个代码清单中被传递到哪里?

回答by Stephen Curran

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. This could be where you are going wrong.

不要忘记回显从 form_label 和 form_input 返回的内容。这可能是你出错的地方。