Welcome Guest, Not a member yet? Register   Sign In
Using set_value() from Form Validation library
#1

[eluser]beachbum[/eluser]
hey errrrybody!

I am currently building an application system on one our sites that takes users through a wizard-like, multiple-page application form.

Basically, the users can create a new application and then return to edit it later.

The problem I'm encountering that I am not sure how to solve is that, during the edit process, I need for the fields in each form to be populated with data from MySQL initially (i.e. a GET request), and then I need to use set_value() to populate those same fields when the user submits the form via POST, so I can continue using the Form Validation library, which I dearly love because it has simplified tedious form validation tasks for me.

Can anyone suggest the best way to accomplish this? I know how I'd pull it off if I weren't using Code Igniter (just plain PHP), but I'm hoping to accomplish it using the tools within Code Igniter. Thanks in advance!
#2

[eluser]zutis[/eluser]
Basically what I think you want to do is this:

- When the user arrives to the form, load the data in your controller and pass it to your view that contains the fields.

@ this point your view will have something like this i guess:

Code:
// Where $item1, $item2 and $item3 is data from DB
$data['item1'] = $item1;
$data['item2'] = $item2;
$data['item3'] = $item3;
// ... and so on
$this->load->view('my_form', $data);

and then in your view

Code:
<input type="text" name="item1" value="<?php echo set_value('item1', $item1); ?>" size="50" />
<input type="text" name="item2" value="<?php echo set_value('item2', $item2); ?>" size="50" />
<input type="text" name="item3" value="<?php echo set_value('item3', $item3); ?>" size="50" />

This will pre-fill the fields from the DB.
#3

[eluser]beachbum[/eluser]
Thank you for such a fast reply!
I have actually got the forms populated with the data from the database already.

My problem is, I need to set the source of the data to the database if the page is requested via GET (i.e. the first time the user loads the form), and set the data source to the return value of set_value() if the page is requested via POST.
#4

[eluser]zutis[/eluser]
Not sure I understand.

Can't you just check in the controller before you send to view like so:

Code:
if(count($_POST) > 0)
{
  // set data for the view from DB
}
  // else send no data to the view
}
#5

[eluser]zutis[/eluser]
i meant

Code:
if(count($_POST) == 0)
{
  // set data for the view from DB
}
  // else send no data to the view
}
#6

[eluser]beachbum[/eluser]
@zutis

Oh, i see what you mean. Yeah, that would probably work. But I just found an easier solution, fortunately.

It's been a long day, or I'd like to think I would have realized this sooner. You just pass the data from the database into the second parameter of the set_value() function, which makes it default to that value unless the user has POSTed the form, in which case it will perform any form validation stuff it needs to on each field and then repopulate the fields with the POSTed data.
#7

[eluser]Flying Fish[/eluser]
could you do something like this

Code:
if ($this->input->post('field_name'))
{
$field_default = $this->input->post('field_name');
}
else
{
$field_default = "whatever";
}

then this
Code:
$field_data = array(
'name'        => 'field_name',
'id'          => 'field_name',
'value'       => set_value('field_name', $field_default),
'maxlength'   => '60'
);


echo form_error('when_dates');
#8

[eluser]zutis[/eluser]
[quote author="beachbum" date="1238637038"]@zutis

Oh, i see what you mean. Yeah, that would probably work. But I just found an easier solution, fortunately.

It's been a long day, or I'd like to think I would have realized this sooner. You just pass the data from the database into the second parameter of the set_value() function, which makes it default to that value unless the user has POSTed the form, in which case it will perform any form validation stuff it needs to on each field and then repopulate the fields with the POSTed data.[/quote]


Thats sort of what I had meant in my first reply to you ....

// Where $item1, $item2 and $item3 is data from DB
$data['item1'] = $item1;

...

&lt;?php echo set_value('item1', $item1); ?&gt; <<< second parameter here

Anyways ... glad you got it sorted!




Theme © iAndrew 2016 - Forum software by © MyBB