Welcome Guest, Not a member yet? Register   Sign In
set_value() question?
#1

[eluser]brucebat[/eluser]
Hi all,

I was wondering how set_value() works, I am guessing in the form_helper it stores all the variables that have been entered in the form and if there is a validation error it will preserve the variables when the form is reloaded.

My question is how can I get form elements that have not been created by the form helper to have their values preserved.

The reason I ask this is because I have a special text box that can only be created using javascript however wheneve I submit my form and the validation fails and the controller reloads my view I lose the values in those specific fields.

Is there any way I can preserve them?

Thanks
#2

[eluser]Andreas Karlsson[/eluser]
set_value() is just a shortcut for $this->input->post() really.
Set a hidden field with the value from set_value() and get that into your text box with javascript.
#3

[eluser]brucebat[/eluser]
Thanks I will try that tomorrow
#4

[eluser]Aken[/eluser]
set_value() is more than just a shortcut - because it checks for existing values, replaces it with any updated values, and allows you to set a default if none exists in the first place.

Because you're using Javascript (a client-side language) to create the element, there's no way to use the set_value() function along with it.

What I'd recommend is to generate the text box in your HTML along with everything else, but keep it hidden and only show it with Javascript instead of generating it completely. You'll have to make sure to use/ignore the value when appropriate, of course.

What Andreas recommended will also work, just not how I'd go about it. Up to you, though!
#5

[eluser]brucebat[/eluser]
Thanks,

My form is Dynamic so I could have x amount of fields generated by javascript. How should I pass the values back?

What do you think about this?

Controller:
Code:
for i < completedrows)
$timevalues[i] = $this->input->post('timefield'.$i);

//pass array back to page

View:

Code:
$i= 0;
foreach ($timevalues)
echo form_hidden ('timetemp'.$i, set_value ($timevalues));

Opinions?
#6

[eluser]Aken[/eluser]
If they've already generated form items and input values, if you're going to display them back to the user you might as well generate the actual items in the view, instead of using hidden inputs.
#7

[eluser]brucebat[/eluser]
Aken,

So what your saying is just put the values straight into the inputs using setvalue?

Code:
echo form_input ('time'.$i, set_value ($returnedtime));

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB