Welcome Guest, Not a member yet? Register   Sign In
What is the difference between set_value() and $this->input->post()?
#1

[eluser]Peter Lachky[/eluser]
Hi everybody,

I am coding a CRUD application and I need, of course, re-populate the form in case the validation fails. For text fields I use this code in my view:
Code:
<input type="text" name="name" id="name" value="<?php echo set_value('name') ?>" />
However, I can use this code with the very same effect:
Code:
<input type="text" name="name" id="name" value="<?php echo $this->input->post('name') ?>" />

My question is what is the point/advantage of using set_value() function? Am I missing anything?

The problem is actually a little bit complicated as I am trying to use the same form for add and edit functionality, so the actual code reads:

Code:
<input type="text" name="name" id="name" value="<?php echo set_value('name') ? set_value('name') : $name ?>" />

where $name is a variable containg the database value in case of editing.

Thanks for advices.
#2

[eluser]cahva[/eluser]
Well the advantage using set_value is that you can use the second parameter to set the default value for that. In this situation that is actually what you want:

Code:
<input type="text" name="name" id="name" value="<?php echo set_value('name',$name) ?>" />

In that, $name would come from controller(and all other variables). If its new record you are creating, those would ofcourse be empty. If you are editing, they have the data from your database.
#3

[eluser]Peter Lachky[/eluser]
Oh, I feel soooo stupid now! :-)

Thanks a lot!
#4

[eluser]Peter Lachky[/eluser]
Just quick note which may help someone.

There is another significant difference: It appears that set_value only works for fields which goes through form_validation->set_rules function.
#5

[eluser]Constantin Iliescu[/eluser]
Thanks, Peter!
The quick note did help me. Smile




Theme © iAndrew 2016 - Forum software by © MyBB