Welcome Guest, Not a member yet? Register   Sign In
data to views
#1

[eluser]mmarcus44[/eluser]
i have a form in my view that should get prepopulated if it has data coming into it example

Code:
<input type="text" class="input" name="first_name" value="<?php echo $first_name ?>"/>

but obviously if $first_name is not defined, then code igniter will throw an error saying so.

isnt there a way to get around this automatically? or do i always have to check to see if its defined first like this:

Code:
<input type="text" class="input" name="first_name" value="<?php if(isset($first_name)) echo $first_name ?>"/>
#2

[eluser]mmarcus44[/eluser]
im trying the helpers too like this

Code:
<?php echo form_input('first_name', $first_name, 'class="input"'); ?>

but if its not defined then it complains still
#3

[eluser]obiron2[/eluser]
Build an array that contains all of the form fields
If necessary, populate the array with values from your database (or model), otherwise set the value to NULL
Populate your initial form with the values from the array

When the $_POST comes back in, you can compare the fields in it to the original array (not all form fields will get returned in the $_POST - e.g. unchecked checkboxes) and this is the easiest way I have found to validate the form.

You can then extend the concept to build a generic form handling process that takes the array, populates the form, validates, represents if there are errors or updates the database if all is OK.

Obiron
#4

[eluser]Marcelo Kanzaki[/eluser]
Why dont you use the set_value() function from the form_validation class?

Read the user_guide to see how it works.

then you would do this in your views:

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

As a first parameter, pass the field name. Seccond parameter is your default value. Now your field will have a default value and it will also be repopulated if the form submission is not done yet.




Theme © iAndrew 2016 - Forum software by © MyBB