在 codeigniter 中单击“确定”警报之前的重复视图

Duplicated view before clicking OK alert in codeigniter

我的代码中有一个小问题,在我发送表单和弹出警报的那一刻,视图在下面重复,我无法解决这个问题。

控制器:

function edit_profesor($id_profesor) {

    $id_profesor_submit = (int) $this->input->post('id_profesor', TRUE);

    if ($id_profesor_submit > 0) {

        $profesor_data['cedula'] = $this->input->post('cedula', TRUE);
        $profesor_data['nombre'] = $this->input->post('nombre', TRUE);
        $profesor_data['correo'] = $this->input->post('correo', TRUE);
        $profesor_data['telefono'] = $this->input->post('telefono', TRUE);
        $profesor_data['id_nivel'] = $this->input->post('nivel', TRUE);

        $result = $this->profesor->edit_profesor($profesor_data, $id_profesor_submit);
        if ($result) {
           $dato = array("message"=>"Se modificó el profesor exitosamente");
        } else {
           $dato = array("message1"=>"Error al intentar modificar profesor");
        }

        $this->load->view('profesor/add_profesor', $dato);
    }
    $result = $this->profesor->gets_profesor($id_profesor);
    $data['profesor'] = $result[0];
    $this->load->view('profesor/add_profesor', $data);
}

型号:

function edit_profesor($profesor_data, $id_profesor) {
    $result = $this->db->get_where('profesor', array('id_profesor =' => "$id_profesor"));
    $return = null;
    if ($result->num_rows() > 0) {
        $this->db->where('id_profesor', $id_profesor);
        $this->db->update('profesor', $profesor_data);

        $user_data = array(
            'correo' => $profesor_data["correo"]
        );
        $this->db->where('id_profesor', $id_profesor);
        $this->db->update('users', $user_data);
        $return = TRUE;
    } else {
        $return = FALSE;
    }
    return $return;
}
function edit_profesor($id_profesor) {

    $id_profesor_submit = (int) $this->input->post('id_profesor', TRUE);

    if ($id_profesor_submit > 0) {

        $profesor_data['cedula'] = $this->input->post('cedula', TRUE);
        $profesor_data['nombre'] = $this->input->post('nombre', TRUE);
        $profesor_data['correo'] = $this->input->post('correo', TRUE);
        $profesor_data['telefono'] = $this->input->post('telefono', TRUE);
        $profesor_data['id_nivel'] = $this->input->post('nivel', TRUE);

        $result = $this->profesor->edit_profesor($profesor_data, $id_profesor_submit);
        if ($result) {
           $dato = array("message"=>"Se modificó el profesor exitosamente");
        } else {
           $dato = array("message1"=>"Error al intentar modificar profesor");
        }

        $this->load->view('profesor/add_profesor', $dato);
    }else{
    $result = $this->profesor->gets_profesor($id_profesor);
    $data['profesor'] = $result[0];
    $this->load->view('profesor/add_profesor', $data);
}
}

您没有放置 else 条件,所以它被加载了两次。

基本上发生的事情是当您提交表单时,if statement 得到 true 并且您 第一次 再次加载视图视图在 if statement 之外加载 第二次 。添加 else 解决您的问题

function edit_profesor($id_profesor) 
{
    $id_profesor_submit = (int) $this->input->post('id_profesor', TRUE);

    if ($id_profesor_submit > 0) 
    {
        $profesor_data['cedula'] = $this->input->post('cedula', TRUE);
        $profesor_data['nombre'] = $this->input->post('nombre', TRUE);
        $profesor_data['correo'] = $this->input->post('correo', TRUE);
        $profesor_data['telefono'] = $this->input->post('telefono', TRUE);
        $profesor_data['id_nivel'] = $this->input->post('nivel', TRUE);

        $result = $this->profesor->edit_profesor($profesor_data, $id_profesor_submit);
        if ($result) {
           $dato = array("message"=>"Se modificó el profesor exitosamente");
        } else {
           $dato = array("message1"=>"Error al intentar modificar profesor");
        }

        $this->load->view('profesor/add_profesor', $dato);
    }
    else
    {
        $result = $this->profesor->gets_profesor($id_profesor);
        $data['profesor'] = $result[0];
        $this->load->view('profesor/add_profesor', $data);
    }
}