MVC - edit form not working - codeigniter

Asked

Viewed 160 times

1

I have a form printing a user’s data. I want the user to be able to edit that data. But it is giving me an error in the view:

Undefined variable: list e Invalid argument supplied for foreach().

My code is this::

Controller:

public function actualizar_perfil()
{ 
    $this->load->helper('url');

    //definição da lista de regras a aplicar
    $config=array(
        array(
        'field' => 'nome',
        'label' => 'nome',
        'rules' => 'required|min_length[0]|max_length[50]'
        ),
        array(
        'field' => 'telefone',
        'label' => 'telefone',
        ),
        );

    $this->load->helper('url');

        $this->load->library('form_validation');
        $this->form_validation->set_rules($config);

        if(($this->form_validation->run())==FALSE)
        { 
            $id = $this->uri->segment(3,0); 
            $this->load-> model('perfil_model');
            $data['username'] = $this->session->userdata('username');
            $data['login'] = $this->perfil_model->GetDisplayableByUsernameAP($this->input->post('id'));
            $this->load->library('form_validation');
            $this->load->view('perfil_view',$data);     
        }
        else { 
            $this->load->helper('url');
            $this->load->model('perfil_model');

            $this->perfil_model->actualizarperfil($this->input->post('id'));

            redirect('controller/perfil');
        }
}

Model:

function actualizarperfil($id)
{   $username = $this->session->userdata('username');
    $data['nome'] = $this->input->post('nome');
    $data['telefone'] = $this->input->post('telefone');

    $this->db->where('id_login', $id);
    $this->db->update('login', $data);
}


function GetDisplayableByUsernameAP($id)
{
    $this->db->select('id_login, nome, username, password, cod_postal, telefone, email, localidade, rua');

    $result = $this->db->get_where('login',array('id_login' => $id));

    return $result->row();
}

View:

<?php echo validation_errors() ?>
  <?php echo form_open('controller/actualizar_perfil') ?>
     <?php foreach ($list as $login): ?>
    <div class="form-group">
      <label class="col-lg-3 control-label">Nome:</label>
      <div class="col-lg-8">
        <input class="form-control" id="nome" value="<?php echo $login->nome?>" type="text">
      </div>
    </div> 
    <div class="form-group">
      <label class="col-lg-3 control-label">Telefone:</label>
      <div class="col-lg-8">
        <input class="form-control" id="telefone" value="<?php echo $login->telefone?>" type="text">
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label"></label>
      <div class="col-md-8">
        <a href="actualizar_perfil/<?php echo $login->id_login?> " style="margin-left: 10px; padding:10px;">Guardar</a>
        <span></span>
        <input class="btn btn-default" value="Cancelar" type="reset">
      </div>
    </div>
   <?php endforeach ?>  

In the Controller he comes in here: form_validation->run())==FALSE;. Does not enter the else.

1 answer

0

Has several errors:

View:

You forgot to put your name (name) of inputs:

<?php echo validation_errors(); ?>
<?php echo form_open('controller/actualizar_perfil'); ?>
 <?php foreach ($list as $login): ?>
    <div class="form-group">
      <label class="col-lg-3 control-label">Nome:</label>
      <div class="col-lg-8">
        <input class="form-control" id="nome" name="nome" value="<?php echo $login->nome;?>" type="text">
      </div>
    </div> 
    <div class="form-group">
      <label class="col-lg-3 control-label">Telefone:</label>
      <div class="col-lg-8">
        <input class="form-control" id="telefone" name="telefone" value="<?php echo $login->telefone;?>" type="text">
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label"></label>
      <div class="col-md-8">
        <a href="actualizar_perfil/<?php echo $login->id_login;?> " style="margin-left: 10px; padding:10px;">Guardar</a>
        <span></span>
        <input class="btn btn-default" value="Cancelar" type="reset">
      </div>
    </div>
  <?php endforeach; ?>

that is to say, name="nome" and name="telefone" is the information where PHP can retrieve information typed us inputs.

Controller:

Failed to place the check of phone rule, you forgot to put rules in the type variable array $config. It was placed in the example below required (mandatory data), but, you can add the other rules, see in example:

public function actualizar_perfil()
{ 
    $this->load->helper('url');

    //definição da lista de regras a aplicar
    $config = array(
        array(
            'field' => 'nome',
            'label' => 'nome',
            'rules' => 'required|min_length[0]|max_length[50]'),
        array(
            'field' => 'telefone',
            'label' => 'telefone',
            'rules' => 'required'),
        );

    $this->load->helper('url');

    $this->load->library('form_validation');
    $this->form_validation->set_rules($config);

    if(($this->form_validation->run())==FALSE)
    { 
        $id = $this->uri->segment(3,0); 
        $this->load-> model('perfil_model');
        $data['username'] = $this->session->userdata('username');
        $data['login'] = $this->perfil_model->GetDisplayableByUsernameAP($this->input->post('id'));
        $this->load->library('form_validation');
        $this->load->view('perfil_view',$data);     
    }
    else { 
        $this->load->helper('url');
        $this->load->model('perfil_model');

        $this->perfil_model->actualizarperfil($this->input->post('id'));

        redirect('controller/perfil');
    }

}

In the code may have some more errors sintaxe, check all problems correctly and start refactoring your code. These two tips will solve a good part, but, your code is without standard and it is difficult to solve all problems. Remember that, a well written and organized code, helps in maintenance.

Generally speaking, the request and validation of information of your form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.