Welcome Guest, Not a member yet? Register   Sign In
repopulate form if source array doesnt exist
#6

[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' Big Grin[/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:
    &lt;?php echo form_input('lastname', set_value('lastname', $client['lastname']); ?&gt;


&lt;?php echo form_close(); ?&gt;
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.


Messages In This Thread
repopulate form if source array doesnt exist - by El Forum - 10-06-2010, 04:42 PM
repopulate form if source array doesnt exist - by El Forum - 10-06-2010, 06:50 PM
repopulate form if source array doesnt exist - by El Forum - 10-07-2010, 08:07 AM
repopulate form if source array doesnt exist - by El Forum - 10-07-2010, 08:37 AM
repopulate form if source array doesnt exist - by El Forum - 10-07-2010, 08:44 AM
repopulate form if source array doesnt exist - by El Forum - 10-07-2010, 09:34 AM
repopulate form if source array doesnt exist - by El Forum - 10-07-2010, 11:02 AM
repopulate form if source array doesnt exist - by El Forum - 10-12-2010, 09:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB