Error codeigniter 3: Undefined variable: alert

3

I'm doing the recording in the bank received from a form, it's recording, but I want a message to appear after the recording, which was received successfully. But you are giving the error below. I've been locked in this for 2 days, I do not know how to solve it. Or some suggestion to change the recording, for json, I'm open to suggestions.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: alerta

Filename: public/contato.php

Line Number: 72

Backtrace:

File: D:\xampp\htdocs\alertweb\application\views\public\contato.php
Line: 72
Function: _error_handler

File: D:\xampp\htdocs\alertweb\application\views\public\index.php
Line: 7
Function: include

File: D:\xampp\htdocs\alertweb\application\controllers\Inicio.php
Line: 45
Function: view

File: D:\xampp\htdocs\alertweb\index.php
Line: 292
Function: require_once

here is the controller

public function Contato(){

    $mensagem = null;


    if($this->input->post('enviarContato') === 'enviarContato'){

        $nome = $this->input->post('nome');
        $email = $this->input->post('email');
        $telefone = $this->input->post('telefone');
        $assunto = $this->input->post('assunto');
        $texto = $this->input->post('texto');

        $this->load->model('InicioModel');
        $this->InicioModel->gravarContato($nome,$email,$telefone,$assunto,$texto);

        if($this->InicioModel->gravarContato === true){
            $mensagem = array('class' => 'success',
                    'mensagem' => 'Mensagem enviada com sucesso, retornaremos em breve.'
                );
        }else{
                $mensagem = array('class' => 'danger',
                    'mensagem' => 'Ocorreu um erro! Favor, tentar mais tarde.'
                );
        }
    }

    $dados = array('alerta' => $mensagem);

    $this->load->view('public/index', $dados);

}

and the view

<div class="col-md-9 col-xs-9 col-lg-9 col-sm-9">
                <form class="form-horizontal" action="<?php echo base_url('Inicio/contato');?>" method="post">
                    <fieldset>

                    <!-- Text input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="nome">Nome</label>  
                          <div class="col-md-5">
                          <input id="nome" name="nome" type="text" placeholder="Digite o seu nome" class="form-control input-md" required="">

                          </div>
                        </div>

                        <!-- Text input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="email">E-mail</label>  
                          <div class="col-md-5">
                          <input id="email" name="email" type="text" placeholder="Digite o seu e-mail" class="form-control input-md" required="">

                          </div>
                        </div>

                        <!-- Text input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="telefone">Telefone</label>  
                          <div class="col-md-5">
                          <input id="telefone" name="telefone" type="text" placeholder="Digite o seu telefone" class="form-control input-md" required="">

                          </div>
                        </div>

                        <!-- Text input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="assunto">Assunto</label>  
                          <div class="col-md-5">
                          <input id="assunto" name="assunto" type="text" placeholder="Digite o assunto do contato" class="form-control input-md" required="">

                          </div>
                        </div>

                        <!-- Textarea -->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="texto"></label>
                          <div class="col-md-5">                     
                            <textarea class="form-control textarea" id="texto" name="texto" style="height: 200px;"></textarea>
                          </div>
                        </div>

                        <div class="form-group">
                          <label class="col-md-4 control-label" for="texto"></label>
                          <div class="col-md-5">                     
                            <button class="btn btn-default" type="submit" name="enviarContato" value="enviarContato">Enviar <i class="fa fa-envelope-o"></i></button>
                          </div>
                        </div>

                        <?php if($alerta != null){?>
                        <div class="form-group">
                          <div class="col-md-12">
                          <div class="col-md-4"></div>
                            <center>
                                <div class="col-md-4">
                                    <div class="alert alert-<?php echo $alerta['class'];?>">
                                    <?php echo $alerta['mensagem'];?>
                                    </div>
                                </div>
                            </center>

                            </div>

                          </div>
                        <?php }?>
                    </fieldset>
                </form>
            </div>
        </div>
    
asked by anonymous 09.09.2015 / 21:33

2 answers

4

$alerta does not exist in the view because in this if, the key name in $dados will be defined from the array definition, the alert key will not find, since the unique key was class

if($this->InicioModel->gravarContato === true){
   $mensagem = array('class' => 'success' ...
}else{
   $mensagem = array('class' => 'danger'
}

The correct one would be:

$dados['alerta'] =  $mensagem;
    
09.09.2015 / 21:41
2

Use flashdata() to create these messages:

$this->session->set_flashdata('mensagem', 'Contato enviado com sucesso');

and in the view check for: if( $this->session->flashdata('mensagem') )

Do not forget to start the CI session library.

More information: link

    
09.09.2015 / 21:46