Shashank Gaurav Shashank Gaurav - 4 months ago 8
HTML Question

How to send php variable from view(html) to controller

This is my html code. I want to pass "

$category_edit->c_name
" value to my
Update()
controller. I am getting "
$category_edit
" variable from another controller.

I am using CodeIgniter framework.

<form method="post" action="<?php echo base_url('admin/category/update');?>">
<label>Parent Category: </label></br>
<select name="parent_id">
<?php
echo '<option value="' .$category_edit->id .'">';
echo $category_edit->p_name;
echo '</option>';
?>
</select>
<label>Category</label>
<input type="text" name="<?php echo $category_edit->c_name; ?>" id="category_name" value="<?php echo $category_edit->c_name; ?>">
<button>Update</button>
</form>


This is my
update()
controller.
I am getting Error:


  1. Undefined variable: category_edit

  2. Trying to get property of non-object

    public function update(){
    $this->load->model('category_model');
    echo $category_edit->c_name;
    }


Answer

Please kindly check this reference code:

public function update_view()
{
    $this->load->model('category_model');

    $data['category_edit'] = $this->category_model->get_category_values(); // return array
    $data['extra_variable'] = 'lorem ipsum';

    $this->load->view('category/update_view', $data);
}

at your category/update_view.php :

<form method="post" action="<?php echo base_url('admin/category/update');?>">
 <label>Parent Category: </label></br>
 <select name="parent_id">
 <?php
    echo '<option value="' .$category_edit['id'] .'">';
    echo $category_edit['p_name'];
    echo '</option>';
 ?>
 </select>
 <label>Category</label>
 <input type="text" name="<?php echo $category_edit['c_name']; ?>" id="category_name" value="<?php echo $category_edit['c_name']; ?>">
 <button>Update</button>
</form>

EDIT:

Please refer: http://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view