how to get the value of form input box in codeigniter
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(); ?>
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.
Where does the $data array get passed to in the third code listing?
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.