Welcome Guest, Not a member yet? Register   Sign In
Form helper Set_value function

Hi Guys,

Now I'm not sure if im barking up the wrong tree here, but I am trying to do a little as possible (as normal Smile) but have ended up spending ages trying to work this out!

I have a view file I want to use for inputing and editing data.

<?php echo validation_errors(); ?>
<h2>Employee stuff updated to old record</h2>
Employeeid:&lt;input type="text" name="employeeid" value="&lt;?php echo set_value('employeeid'); ?&gt;" /&gt;&lt;br />
Surname:&lt;input type="text" name="surname" value="&lt;?php echo set_value('surname'); ?&gt;" /&gt;&lt;br />
Title:&lt;input type="text" name="title" value="&lt;?php echo set_value('title'); ?&gt;" /&gt;&lt;br />
Initials:&lt;input type="text" name="initials" value="&lt;?php echo set_value('initials'); ?&gt;" /&gt;&lt;br />
Firstname:&lt;input type="text" name="firstname" value="&lt;?php echo set_value('firstname'); ?&gt;" /&gt;&lt;br />
Address1:&lt;input type="text" name="address1" value="&lt;?php echo set_value('address1'); ?&gt;" /&gt;&lt;br />
Address2:&lt;input type="text" name="address2" value="&lt;?php echo set_value('address2'); ?&gt;" /&gt;&lt;br />
Address3:&lt;input type="text" name="address3" value="&lt;?php echo set_value('addrress3'); ?&gt;" /&gt;&lt;br />
Postcode:&lt;input type="text" name="postcode" value="&lt;?php echo set_value('postcode'); ?&gt;" /&gt;&lt;br />
&lt;input type="submit" /&gt;&lt;br />

This works fine for adding and validating new records. However, I assumed that because the set_value function uses the $_POST variable I could do this:

public function edit($id)
  //function to edit employees


  if ($this->form_validation->run() == FALSE)
   $query = $this->employees_m->get_one_employee($id);

   if ($query->num_rows() == 1)
    $new = $query->result_array();
   //Set array to POST array for processing
   $_POST = $new[0];
   $old = array_shift($_POST);
  } else {
   $data = $this->input->post(NULL, TRUE);
   //set not inputed data
   $this->employees_m->update('employee_data', $data);
   echo 'Success';

The $_POST array displays ok on the view page with print_r but set_value doesn't seem to use it and returns blank fields.

Have I done something obviously wrong or just an idiot for trying something it cannot do!!



It's generally best practice to not deal directly with the $_POST array in CI apps. If you want to include default values in a form, use the second parameter of set_value().

Thanks Aken I do fully understand that but I think the point is being missed.

For 'default' values, yes set value is the best option. But the values are being returned from a db and I am trying to use the same view for both returned values and default values.

You're trying to get values from your database to be in your form upon first loading it. To do that, you need to use the second parameter of set_value().

I've been an idiot!!

Thinking aloud so bare with me :|

So if I get a database return to say the $db var.

Then using:
&lt;?php echo set_value('address1',@$db['address1']); ?&gt;
It will add the db value in or if the $db var is null or non-existent would just display nothing ignoring the error!!

Thanks Aken, sometimes we all need nudge!!

PS. Like you website by the way!

There ya go :-p I wouldn't use the error suppression, I would add code to verify that the particular items exist (even if it means creating default blank ones) before passing the array to the view. Error suppression is cheap and not a great coding style.

And thank you Smile

Theme © iAndrew 2016 - Forum software by © MyBB