【发布时间】:2014-09-02 18:14:37
【问题描述】:
在做项目时遇到一些问题。这就是正在发生的事情:通过选择我选择我想要编辑的用户。一个表格包含该用户发布的所有消息。单击编辑后,使用 Ajax 会出现一个带有可编辑字段的窗口。然后,提交信息后,会出现以下消息:
消息:未定义变量:数据
目前,在提交之后,我添加了一个重定向,通过用户 ID 的 url 再次将我发送到表格视图。
下面是更能说明情况的代码:
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);
}
重定向到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');
}
}
最后,模型中失去价值的函数:
function mensajes($nombre){
$this->db->where('id', $nombre);
$query = $this->db->get('mensajes');
foreach ($query->result() as $fila)
{
$data[] = $fila;
}
return $data;}
如果我尝试直接访问函数而不是先通过选择表单,则会出现同样的问题。
选择要编辑的用户后,如何返回表格视图?
编辑:添加重定向到 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
}
【问题讨论】:
-
通过 set_flashdata() 设法让它“工作”。但老实说,我相信这不是问题的真正解决方案。显然,如果用户刷新,数据丢失,未定义变量消息重新出现。
标签: php codeigniter variables redirect