Welcome Guest, Not a member yet? Register   Sign In
Variable not set when post request
#1

hi this is my template :


Code:
<?= $this->include('templates/header_admin') ?>

<?= $this->include('templates/menu_admin') ?>
   <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <?= $title_h1 ?>
      </h1>

    </section>


    <?= $this->renderSection('content') ?>
      </div>
  <!-- /.content-wrapper -->

      <footer class="main-footer">
        <div class="pull-right hidden-xs">
          <b>Version</b> 2.4.0
        </div>
        <strong>Copyright &copy; 2020-<?= date('Y') ?> <a href="https://mywebsite">Name of project</a>.</strong> All rights
        reserved.
      </footer>


  <!-- Add the sidebar's background. This div must be placed

       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->
   
<?= $this->include('templates/footer_admin') ?>
    
    <?= $this->renderSection('script') ?>

<?= $this->include('templates/end_html') ?>


this is my method to change Admin's email :

PHP Code:
public function changeEmail()
    {
        $data=[];

        $model = new AdminModel();    

        $data
['title_h1'] = 'Cambia Email';

        $validation =  \Config\Services::validation();


        if($this->request->getMethod() === 'post')
        {

          


          $rules
=[

                                'email'=>[
                                             'label'=>'Email',
                                             'rules'=>'required|trim|min_length[0]|max_length[100]|valid_email|validateEmailDuplicateAdmin[email,id]',
                                             'errors'=>[
                                                        'min_length'=>'Lunghezza minima Email 0 caratteri',  
                                                        
'max_length'=>'Lunghezza massima Email 100 caratteri',
                                                        'required'=>'Email obbligatorio',
                                                        'valid_email'=>'Inserisci una mail valida ',
                                                        'validateEmailDuplicate' =>'Mail gia` presente'
                                                       ]
                                          ],

                                'ripeti_email'=>[
                                             'label'=>'Ripeti Email',
                                             'rules'=>'required|trim|min_length[0]|max_length[100]|valid_email|matches[email]',
                                             'errors'=>[
                                                        'min_length'=>'Lunghezza minima Email 0 caratteri',  
                                                        
'max_length'=>'Lunghezza massima Email 100 caratteri',
                                                        'required'=>'Email obbligatorio',
                                                        'valid_email'=>'Inserisci una mail valida ',
                                                        'matches' =>'Ripeti email e Email non coincidono'
                                                       ]
                                          ],
                                
                  
];


          if($this->validate($rules))
          {

              $post $this->request->getPost();

              $data=[

                        'email' => $post['email']

                    ];

              $res=$model->update($_SESSION['admin_id'],$data);


              if($res) {

                $admin $model->where('email',$this->request->getVar('email'))->first();

                $this->setAdminSession($admin);

                session()->setFlashdata('success','Mail correttamente modificata');

                $data['page_to_redirect'] = '/admin/index';

              }else{

                session()->setFlashdata('error','Problemi modifica Mail');


              }

          }else {
             
            $data
['validation'] = $validation;
           
          
}

        }


        echo view('empty_view',$data);
        echo view('/admin/changeEmail');

    


this is the form :

PHP Code:
<?= $this->extend('templates/layout_admin'?>
<?= $this
->section('content'?>


 <!-- Main content -->
    <section class="content text-center">

      <!-- Default box -->
      <div class="box">
        <div class="box-header with-border">

              
                      <?php if(!empty($validation)) :?>
                      <div class='col-md-12 text-center'>

                        <div class='alert alert-danger'>
                          <span class='login_error'><?= $validation->listErrors(); ?></span>
                        </div>


                       </div>

                      <?php endif; ?>


                      <?php if (session()->get('success')): ?>

                                  <div class='col-md-12 text-center'>

                                    <div class='alert alert-success' role="alert">
                                      <span class=''><?= session()->get('success'); ?></span>

                                    </div>


                                  </div>            
                      <?php 

                           header
("Refresh:3 ; url=$page_to_redirect");

                      ?>

                      <?php endif ?>
            
                      <?php if (session()->get('error')): ?>

                                  <div class='col-md-12 text-center'>

                                    <div class='alert alert-success' role="alert">
                                      <span class=''><?= session()->get('error'); ?></span>

                                    </div>


                                  </div>            

                      <?php endif ?>

                 <?php
                $attributes 
= array('class' => 'form-horizontal',
                             'id' => 'myform',
                             'name' => 'form_cambio_email',
                             'method' => 'POST',

                            );
                echo form_open ('/admin/changeEmail',$attributes);

                     ?> 

              <input type="hidden" name="id" value="<?= session()->get('admin_id'?>">

              <br>
                    <label>Nuova Email</label> 
                    <br>
                    <input type="email" name="email" class="form_control"> 
               <br>

                    <label>Ripeti Nuova Email</label> 
                    <br>
                    <input type="email" name="ripeti_email" class="form_control"> 
               <br>
               <br>
               <br>


               <button class="btn btn-info" type="submit">Modifica Email</button>
        </div>

      </div>
      <!-- /.box -->

    </section>
    <!-- /.content -->




  <?= $this->endSection() ?>


empty_view it' an empty file only to send variable to view.

when i go to /admin/changeEmail title_h1 is set but when i post the form i have : 

Undefined variable: title_h1 

Why ? $data['title_h1'] is set at the start of method ...
Reply
#2

Because you are overwriting it.

PHP Code:
$data=[
    
'email' => $post['email']
]; 
Reply
#3

(08-30-2020, 07:58 AM)jreklund Wrote: Because you are overwriting it.

PHP Code:
$data=[
    'email' => $post['email']
]; 

sorry it's true....thank's
Reply




Theme © iAndrew 2016 - Forum software by © MyBB