Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
    
like image 237
Shashank Gaurav Avatar asked Jan 24 '26 17:01

Shashank Gaurav


2 Answers

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

like image 108
Ronak Patel Avatar answered Jan 26 '26 09:01

Ronak Patel


Make some changes in HTML(See below code)

<input type="text" name="category_name" id="category_name" value="<?php echo $category_edit->c_name; ?>">

public function update()
{
$this->load->model('category_model');
echo $this->input->post('category_name');
}
like image 25
Rajen Antarkar Avatar answered Jan 26 '26 09:01

Rajen Antarkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!