0

I am trying to make an edit page where the form fields are populated from the database. I'm trying to get it working by just setting some random text, but I'm having trouble making it show.

I have the following in my controller:

public function edit($item_id) {

    $this->data['title'] = "Edit Item";

    $this->data['item_title'] = array(
        'name' => 'item_title',
        'id' => 'item_title',
        'type' => 'text',
        'value' => 'a title',
      );

      $this->data['url_slug'] = array(
        'name' => 'url_slug',
        'id' => 'url_slug',
        'type' => 'text',
        'value' => 'some-url-slug',
      );

    $this->template->build('admin/item/form', $this->data);
  }

This is my view:

<?php echo form_open('admin/item/update_item', array('id' => 'item_form')); ?>

<input type="text" name="item_title" value="<?php echo set_value('item_title'); ?>" id="item_title" placeholder="Enter a title..."/>

<input type="text" name="url_slug" value="<?php echo set_value('url_slug'); ?>" id="url_slug" placeholder="url-slug-of-the-item"/>

When I go the /edit/id page, the placeholder is still showing and the value is blank. Why is not setting? It works fine when I use it for form validation.

2
  • item_title and url_slug are arrays but value attribute must be of string. Commented Jul 27, 2012 at 19:16
  • Possible duplicate of stackoverflow.com/questions/3071774/… Commented Jul 27, 2012 at 19:23

3 Answers 3

2

I'm still new to codeigniter, so forgive my ignorance.

Can't you simply use the following?

<input type="text" name="item_title" value="<?= $item_title['value'] ?>" id="item_title" placeholder="Enter a title..."/>

Note: This would be valid for fuelphp; not 100% sure about codeigniter.

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

2 Comments

ya, this could work. I'm trying to use the same view for both creating and editing, so I'm trying to use set_value if possible.
@MotiveKyle regardless, you're using set_value() on an array, rather than the index for its value. Try here: stackoverflow.com/questions/3071774/…
1

I ended up ditching the array and just doing $this->data['item_title'] = 'some title'; in the controller, and in the the view, set_value actually accepts a 2nd parameter, like this:

<?php echo set_value('item_title', $item_title); ?>

This does the trick, although I do get warnings if the variable doesn't exist, so I need to declare them all.

Comments

0

I ended up ditching the array and just doing $data['fullName'] = 'some title'; in the controller, and in the the view, set_value actually accepts a 2nd parameter, like this:

<?php    
   if(!isset($fullname)) {
        $fullname = set_value('fullName');   
   }

   echo form_open(base_url() . "broker/practice_send.html");

   echo form_label("Name", "name");
   $data = array(
      'name'     => 'fullName',
      'id'       => 'name',
      'value'    => set_value('fullName', $fullname)
   );
   echo form_input($data);

?>

Comments

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.