简体   繁体   中英

Codeigniter - Variable undefined after redirect

Having some problems while doing a project. This is what's happening: through a select I pick an user I want to edit. A table comes with all the messages posted by that user. After I click edit, with Ajax a window appears with the editable fields. Then, after submitting the info, the following message appears:

Message: Undefined variable: data

At the moment, after the submit, I added a redirect that sends me again to the table view passing through the url the user id.

Here is the code that shows better the situation:

   function traer_tabla(){ //Here is the function that loads the table after post
     $nombre = $this->input->post('nombre');
     $data['titulo'] = 'Update con codeIgniter';
    $data['mensajes'] = $this->datos_model->mensajes($nombre);
    $this->load->view('datos_view', $data);

}

The function that redirects to traer_tabla

       function actualizar_datos()    
       {        
           $id = $this->input->post('id_mensaje');
           $nombre = $this->input->post('nombre');
           $email = $this->input->post('email');
           $asunto = $this->input->post('asunto');
           $mensaje = $this->input->post('mensaje');

           $actualizar = $this->datos_model->actualizar_mensaje($id,$nombre,$email,$asunto,$mensaje);
           if($actualizar)
           {
            $this->session->set_flashdata('actualizado', 'El mensaje se actualizó correctamente');
            redirect(base_url().'datos/traer_tabla/'.$id, 'refresh'); 
           }
}

And finally, the function in the model that is losing its value:

function mensajes($nombre){        
   $this->db->where('id', $nombre);
$query = $this->db->get('mensajes');
    foreach ($query->result() as $fila)
    {
        $data[] = $fila;
    }
return $data;}

The same problem appears if I try to access directly the function instead of passing first through the select form.

How can I return to the table view after I selected the user I want to edit?

EDIT: added the form that redirects to traer_tabla()

  function mostrar_datos()
{

    $id = $this->input->post('id'); //This post is from the table when the user click edit button

    $edicion = $this->datos_model->obtener($id);

        $nombre = array(
            'name' => 'nombre',
            'id' => 'nombre',
            'value' => $edicion->nombre
        );
        $email = array(
            'name' => 'email',
            'id' => 'email',
            'value' => $edicion->email
        );
        $asunto = array(
            'name' => 'asunto',
            'id' => 'asunto',
            'value' => $edicion->asunto
        );
        $mensaje = array(
            'name' => 'mensaje',
            'id' => 'mensaje',
            'cols' => '50',
            'rows' => '6',
            'value' => $edicion->mensaje
        );
        $submit = array(
            'name' => 'editando',
            'id' => 'editando',
            'value' => 'Editar mensaje'
        );
        $oculto = array(
            'id_mensaje' => $id
           );

        ?>
        <?= form_open(base_url() . 'datos/actualizar_datos','', $oculto) ?>
        <?= form_label('Nombre') ?>
        <?= form_input($nombre) ?>
        <?= form_label('Email') ?>
        <?= form_input($email) ?>
        <?= form_label('Asunto') ?>
        <?= form_input($asunto) ?>
        <?= form_label('Mensaje') ?>
        <?= form_textarea($mensaje) ?>

        <?= form_submit($submit) ?>
        <?php    
        }    

If you direct the browser to another script, all variables defined by the current script will be destroyed when it has finished executing.

To persist variables across scripts you need use the session or cookies.

script1.php

session_start();
$data['titulo'] = 'Update con codeIgniter';
$_SESSION['data']=$data;
header('location:script2.php');
die();

script2.php

session_start();
echo $_SESSION['data']['titulo'];

As andrew stated above when you redirect you lose variables data so solution for that is storing data in sessions.

In your case what you can do is either use session to store the 'nombre' as i see you are trying to access $nombre = $this->input->post('nombre'); which i guess will not work.

Either store 'nombre' in session like this

$this->session->set_userdata('number',$nombre);

and access it anywhere you want like this:

$this->session->userdata('number');

Also note your redirect is

redirect(base_url().'datos/traer_tabla/'.$id, 'refresh');

that means you are passing parameter 'id' to your function traer_tabla.If you want pass that variable you want to make your function to receive that like this.

function traer_tabla($id,$number)
{
//code goes here
}

Similarly if you don't want to store your number in session you can pass it through url as above.

Feel free to let me know if any doubts.

Got the solution. Instead of using set_flashdata(), I used set_userdata(). Now, it returns to the table, and on refresh, the table continues appearing.

Also added unset_userdata() when I select the user I want to edit.

Thanks for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM