Pergunta

I'm using same form to new client and edit client in Code Igniter. Sometimes I'll include new client so the field must be empty. However, sometimes I'll edit a client and I must put respect value to a field. For example:

echo form_input('client_name', $client_to_edit['client_name']);

How can I use "set_values()" and $client_to_edit['client_name'] to pass data to the field?

Foi útil?

Solução

set_value() is really only needed for form_validation and in this case you'll probably need that too. Basically you need to determine if the form is editing or for a new client, if editing it needs to run a query on the database to return that users data and pass it to a variable.

echo form_input('client_name',set_value
('client_name',($user['client_name'] ? $user['client_name']:'')));

Basically what's happening is if the form is editing you're populating the $user variable in the controller with that users data. The set value statement has 3 options. First if the form is returning from form_validation it sets it to whatever was entered when the form was posted, if there is no post data it then looks to see if $user['client_name'] exists, if it does it uses that, if it doesn't it just returns blank.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top