Welcome Guest, Not a member yet? Register   Sign In
Form Helper Variable Check
#1

[eluser]surf66[/eluser]
Hi, this is my first ever post, sorry if it's a very easy solution but I have been looking at the user guide and other internet sites and cant seem to find my answer. I would just like to know how to generate the following code using the CodeIgniter Form Helper.

Code:
<input type="text" name="title" class="form_input" value="<?php if (isset($title['value'])) echo $title['value']; ?><?php echo set_value('title'); ?>" />

I have used the Form Helper a lot and I understand it but the problem arises when I want to include the little if statement to check if a variable is set.

Thanks in advance,
Tom
#2

[eluser]CroNiX[/eluser]
Code:
<input type="text" name="title" class="form_input" value="<?php echo set_value('title'); ?>" />

If the form hasn't been submitted, the value will be nothing (empty string). If you want to provide a default value that shows up when the form is initially loaded, like $title['value'], use the available 2nd parameter of set_value() to set that. If the form was submitted, the value will be whatever the "title" field was submitted as.
#3

[eluser]surf66[/eluser]
Hi, let me try and explain this, bear with me...

I have a list of books in an admin panel and when the user clicks the edit button next to a particular book they are redirected to the edit_book page. A form displays and all the information from the db is pulled in and echoed in the relative fields. for example the book title is displayed in the title field so the user can edit it.

On my add book form I have used the technique you have stated as that simply refreshes the form and puts back into the form fields what the user typed in and you can then show the errors. However when the user edits the data in the edit form and clicks submit, if an error occurs with one of the inputs, the user is redirected back to the form so that I can show the errors but! I then get errors because the variables which store the book information from when the edit form is first loaded are now unset for want of a better term.

I appreciate this may be hard to understand and I don't want it to sound patronising in any way I'm just tying to explain it in as much detail as possible. Anyway It seems to work for me I just need to know if I can do this same thing using the form helper.
#4

[eluser]royduin[/eluser]
I not really see you're problem.

If you're view looks like this with the inputs:
Code:
<? $default_title = 'Something out of the database'; ?>
<input type="text" name="title" value="<? echo set_value('title',$default_title); ?>" />

And in you're controller you run every field trought the form_validation library like:
Code:
$this->form_validation->set_rules('title', 'Title', 'required');

It just works..
#5

[eluser]surf66[/eluser]
The code I have works fully and I understand what it does, I just cant really explain it on here, I was just looking to see if I could generate the code stated in my question using the form helper, I don't think it matters too much though if i don't use the helper.




Theme © iAndrew 2016 - Forum software by © MyBB