[eluser]Santiago DimattÃa[/eluser]
[quote author="tjmthome" date="1286478461"]Muchas gracias Santiago, but what happens if, in my form the user changed the address or the phone number; if I reload the info from DB, that change will be lost, maybe im not in the right way I need 'the way of the samurai'
[/quote]
This is the way I use:
Controller
Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Clients extends Controller {
public function edit($id = NULL)
{
if(!isset($id) || !is_numeric($id))
{
show_error();
}
$this->load->library('form_validation');
$this->load->helper('form');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
if($this->form_validation->run() === FALSE)
{
$data = array();
$data['client'] = $this->clients_model->get($id);
$this->load->view('form', $data);
}
else
{
$upd = array();
$upd['name'] = $this->input->post('name');
$upd['lastname'] = $this->input->post('lastname');
$this->clients_model->update($id, $upd);
$this->load->view('form_success');
}
}
}
So:
if the form was not sent, or some field is incomplete (both name & lastname are required), it will send the client data from the DB to the view.
If the form was sent, it will update the client with the new $_POST data and load a success view.
View:
Code:
<?php echo form_open('clients/edit/' . $client['id']); ?>
Name:
<input type="text" name="name" value="<?php echo set_value('name', $client['name']); ?>" />
<br />
Last name:
<?php echo form_input('lastname', set_value('lastname', $client['lastname']); ?>
<?php echo form_close(); ?>
In the view, set_value will check if the $_POST data exists. If it does exists, it will print it. If it doesn't, it will print the $client[] value.
Results:
Form wasn't send: Show $client[] value.
Form was sent with all fields valid: Show form_success view.
Form was sent but some field is invalid/missing: Show $_POST and/or $client[] value.