Welcome Guest, Not a member yet? Register   Sign In
Can't re-populate form field in "Edit" form [SOLVED]
#1

[eluser]KeyStroke[/eluser]
Hi,

I'm having a small issue here. I have an "Edit" form where I edit current posts. The problem is that I can't re-populate the form's fields since I have to initially populate it with the post's data from the database. So I end up with something like this:

Code:
<input type="text" name="title" value="<?=$title?>">
Which means I can't use $this->validation->title to re-populate this field in case of error (or so it seems).

Is there anyway around this?


Appreciate your help
#2

[eluser]Michael Wales[/eluser]
Code:
if ($this->validation->title) {
  echo $this->validation->title;
} else {
  echo $title;
}
#3

[eluser]KeyStroke[/eluser]
Thanks. That inspired me to do a simpler solution in the controller:
Code:
// when validation fails
$data['title'] = $this->validation->title;
$data['body'] = $this->validation->body;
// ... and so on
$this->load->view('edit_post', $data);
#4

[eluser]Référencement Google[/eluser]
To make things even more readable you can use also ternary operators:

Code:
<input type="text" name="title" value="<?=isset($this->validation->title) ? $this->validation->title : $title ?>">




Theme © iAndrew 2016 - Forum software by © MyBB