3

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;
    }
    
3
  • $category_edit this is not gloabal variable. Commented Jul 28, 2016 at 10:35
  • check my answer brother Commented Jul 28, 2016 at 10:40
  • add your controller code. Error is Undefined variable: category_edit. It means category_edit Array is empty. Commented Jul 28, 2016 at 12:25

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Error is Undefined variable: category_edit. It means category_edit Array is empty. Your answer is wrong. It won't print any data to view.
dude, its your work now that It must not return empty array as you are doing database work. "OR" make validation for it. "I can show you path, Not complete program". I am absolutely right, this is the same copy from my project.
<?php print_r($category_edit); exit; ?> in your view. see whats printing.
2

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

Comments

1

You need to add name of field static not any variable. so try to add like this

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

and on controller you can get value of it like

$this->input->post('category_name');

3 Comments

Error is Undefined variable: category_edit. It means category_edit Array is empty. Your answer is wrong. It won't print any data to view.
this one is for controller, for view you have to get it.
user has asked from view to controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.