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

[eluser]tjmthome[/eluser]
Hi, maybe you could give me some advise about best way to attack this problem :roll:

I have a view with data with info of users (tabular) name, address, phone etc. at the end of the row two links for 'edit' and 'delete'.
when the user press the link edit, i get the info from a DB and populate a form with the info from that user using an $user array like this:
<input name="name" type=text value="<?=$user['name']"?>/> and so on.
this forms action is pointing to welcome/doUpdate

In function welcome/doUpdate i do validate the data from the user, but the problem is that i dont know how to re-populate the form again if there is an validation error because $user array doesnt exists anymore.

Im sure you know a better way to solve this. I 've been searching in forums and i have no seen someone asking similar question.

Thanks in advance
#2

[eluser]Santiago Dimattía[/eluser]
Why don't you query the user data again in welcome/doUpdate, and then you do this:
Code:
<input name="name" type="text" value="<?php echo set_value('name', $user['name']); ?>" />
So, if the form was sent, it will display $_POST vars. If it wasn't, it will show the default value (in this case, $user['xxx']).
#3

[eluser]tjmthome[/eluser]
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
#4

[eluser]Alexandru M.[/eluser]
What if you store the values in an array in welcome/doUpdate and pass it back to your previous function ? Then in the first function you check if data was passed and repopulate from that or if no data was passed then query the database Smile
#5

[eluser]LuckyFella73[/eluser]
hej,

I'm sure there are still better solutions, here just an other
approach how you can handle that in your controller.
I took a method from one of my projects and deleted a lot of
stuff - it's just to show the principle you can do database
update stuff with forms.

Here the method (controller):
Code:
<?php
function edit()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    if ( isset($_POST['form']) AND $_POST['form'] == 'form_edit' )
    {
        # Data comes from form:
        $data['row_id'] = $this->input->post('row_id');
        
        $data['titel'] = $this->input->post('titel');
        $data['text'] = $this->input->post('text');

        $this->id = $this->uri->segment(3);
        $data['id'] = $this->id;

        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('titel', 'Titel', 'trim');
        $this->form_validation->set_rules('text', 'Text', 'trim');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('jobs/view_jobs_edit.php', $data);
        }
        else
        {
            $query = $this->jobs_model->update_job_de($this->tbl_jobs_de);
            redirect('jobs/list_entries', 'refresh');
        }
    }
    else
    {
        $data['id'] = $this->uri->segment(3);
        
        # Get data from Database and send to view
        $query = $this->jobs_model->get_job_to_edit($this->tbl_jobs_de,$data['id']);
        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $data['titel'] = $row->titel;
                $data['text'] = $row->text;
                $data['anzeigen'] = $row->anzeigen;
            }
        }
        
        $this->load->view('jobs/view_jobs_edit.php', $data);
    }
}?>

In your view file you set (re-)populating
code like this:

Code:
<?php echo set_value('text',$text); ?>


Note that the way of handling the id is not save. This code is from a small
backend where I know that just the client has access to otherwise it could be
quite dangerous to set the id related to the database row by url ...
#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.
#7

[eluser]Bas Vermeulen[/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]

Hi, the solution provided earlier by Santiago:

Code:
&lt;input name="name" type="text" value="&lt;?php echo set_value('name', $user['name']); ?&gt;" /&gt;

Will do what you want... with the second param of the set_value function you can set a default value, but if you submit the form & use the form validation lib & have validation errors CI will remember the updated value and won't show the default one, brilliant isn't it.

Santiago, in your last reply you do:

Code:
if($this->form_validation->run() === FALSE)
        {
            $data = array();
            $data['client'] = $this->clients_model->get($id);
            $this->load->view('form', $data);
        }

I bet there is a view method, why not use:

Code:
if($this->form_validation->run() === FALSE)
        {
            $this->view('id');
            return;
        }
#8

[eluser]tjmthome[/eluser]
Hi All for your valuable comments and suggestions

yes the approach I took was like Santiago said, the trick was in the

form_open('clients/edit/' . $client['id']);

I wasnt adding the $client['id'] to the form, instead i was taking it from segment(3)

thanks a lot guys




Theme © iAndrew 2016 - Forum software by © MyBB