How to get the value of a form input field in codeigniter

FORM INPUT Help value !!

// this is just a refrence from $ nm and $ fid from test_model //

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

Let's say I have one form_view with

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

and function to get one line

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

then in the controller .. index ($ id = 0)

and somewhere in the index

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

and the mysql table has something like 1. victor, 2. visible, etc. as the name value

but here it does not take the name and id value from form_input and does not show it again in form_view in the same input field as the winner, etc., so update and send it back to the database ...

help anyone !! and please be easy as I am new to CI !!

+5
source share
3 answers

, , , , .

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

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

, .

, ( "- " ) $id , "id" ( "name" = > "fname", 'id' = > 'id'), , , , .

$data ?

0

, , .

// 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));

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

, form_label form_input. , .

0
source

All Articles