Welcome Guest, Not a member yet? Register   Sign In
isset set_value second parameter on add/edit page
#1

[eluser]gwerner[/eluser]
This question has been asked before, but I'm not finding anything definitive. I've created a form page that is one view for both add and edit. I'm using set_value for the input value fields. Is using isset the best way to bypass the variable if a user is accessing the add page? I've read a few examples that stated you should blank the variables before loading the view. That seems about the same as my example below, but I could be wrong.

If I don't use the isset it will throw the variable is undefined error. Which makes sense because the variable isn't set like it would be if accessing the edit page. The edit page would query the database and set the value.

I really just want to make sure I'm heading in the right direction here. I have a bunch or forms I have to create and don't want to head down the wrong path and then have to redo everything. Any isight is appreciated!

Code:
value="<?php echo set_value('userFirst', (isset($data['users_first'])) ? $data['users_first'] : '' ); ?>"
#2

[eluser]Aken[/eluser]
What I do is create an object that has the default values of each form item. On the ADD page, the default values are empty strings. On the EDIT page, the default values are whatever is current from the database.

A really rough example:

Code:
<?php

// ADD Controller

$values = (object) array(
'name'    => '',
'email'    => '',
'address'   => '',
'something_else' => '',
);

$viewData = array(
'mode'  => 'add',
'values' => $values,
);

$this->load->view('form', $viewData);

// EDIT Controller

$query = $this->db->query('SELECT * FROM form_values WHERE id = 1');

$viewData = array(
'mode'  => 'edit',
'values' => $query->row(),
);

$this->load->view('form', $viewData);

// VIEW

?>

<form action="" method="post">

<input type="text" name="name" value="<?php echo set_value('name', $values->name); ?>">

</form>
#3

[eluser]gwerner[/eluser]
Thanks! That makes sense and I'll have to give that a try.




Theme © iAndrew 2016 - Forum software by © MyBB