Welcome Guest, Not a member yet? Register   Sign In
Keep post value after submit
#1

[eluser]Linkark07[/eluser]
Hi there.

Currently, having a problem. I'm trying to make an attendance system. The professor has various groups assigned to him, so I decided to use a dropdown that will allow the professor to select the group he wants to see. So far so good; after that, through Grocery Crud, the students assigned to his group appear.

Now, here comes the problem: if the professor edits a student, or tries to filter the list, or use any of the functions of the CRUD, the entire table appears on the list. I'm assuming the variable which is assigned the post variable gets lost after the professor uses one of the functions of the CRUD.

Same could also be said on another form which doesn't uses the CRUD; the administrator chooses a group, then through another view he assigns a student to the list. Instead of returning to the list through a redirect, the application sends the administrator to the choose group page.

Here is the code for the select group view:

Code:
<div id="content">
<h1>Lista de Asistencia</h1>
</br>
Seleccione el grupo que desea ver la Lista de Asistencia<p></p>

</br>

</br>

&lt;?php
$attributes = array('class' => 'bootstrap-frm',
    'name' => 'formulario',
    'id' => 'formulario');
?&gt;
            &lt;?=form_open(base_url().'admin/lista_asistencia_admin', $attributes)?&gt;
              <label>Curso</label><select class="grupo" id="grupo" name="grupo"><p>&lt;?=form_error('grupo')?&gt;</p>
              </br>
            &lt;?php

            foreach($nombre as $row)
            {
              echo '<option value="'.$row->id_grupo.'">'.$row->id_grupo.'</option>';
            }
            ?&gt;
            </select><p></p>
    
            
              &lt;input type="hidden" name="id" readonly/&gt;&lt;p>
              
              &lt;input type="hidden" name="token" value="&lt;?=$token?&gt;" /&gt;
              &lt;input type="submit" name="submit" value="Confirmar Datos" /&gt;
            
               &lt;?=form_close()?&gt;
            
             [removed]
        
         function pasar_valores(){
             $.ajax({
        type: "POST",
        url: "&lt;?php echo base_url();?&gt;admin/lista_asistencia_admin",
        data: $("#formulario").serialize(),
        success: function(data){
         alert("success");
           $('#grupo').val('');
          
          }
          
          ,
       error: function(){
           alert("Fail")
       }
   });
   e.preventDefault(); // could also use: return false;
});
});
            
         }
         [removed]
     </br><p></p>
And the Grocery Crud code
Code:
$grupo = $this->input->post('grupo');
                $this->grocery_crud->where('id_grupo', $grupo); // Here I use as where the post value.
                     $this->grocery_crud->display_as('id_participante', 'Cédula');
                      $this->grocery_crud->display_as('id_grupo', 'Grupo');
        $this->grocery_crud->set_subject('Participante');
        $this->grocery_crud->set_relation('id_participante', 'participante', 'numero_identificacion');
              $this->grocery_crud->set_table('asistencia');
    $this->grocery_crud->edit_fields('clase', 'observaciones');
       $this->grocery_crud->unset_delete();
                $this->grocery_crud->unset_add();
                $this->grocery_crud->unset_export();
                $this->grocery_crud->unset_print();
            $output = $this->grocery_crud->render();

        $this->ver_lista($output);

If the post variable loses its value, how can I keep its value there? Thanks in advance.
#2

[eluser]Flemming[/eluser]
You're right, the post value is only accessible on the first load of the controller/page

You should look at flashdata:

https://ellislab.com/codeigniter/user-gu...sions.html

This allows you to keep data 'alive' after a page reload - by default it only lives for 1 reload but you can extend it. Scroll down to the flashdata section of the page in the userguide and try it out.

If you get stuck you can try searching these forums for 'flashdata'

Hope that helps!
#3

[eluser]Linkark07[/eluser]
Gotcha. Suspected that was the issue.

Gonna text it tomorrow though, decided to change my approach with the list.
#4

[eluser]Linkark07[/eluser]
Flemming, did what you recommended me and it worked if I use set_flashdata(). But if I use keep_flashdata(), it doesn't work.

Edit: changed set_flashdata() to set_userdata() and it is finally working as it should be.

Thanks for everything.
#5

[eluser]Unknown[/eluser]
Hi,

Here is a some code I used to do that, it was to keep post data after a redirect:
I recommend you use flashData, userData is using the cookie, if you don't need to persist flash data is better for one time use


$data['password'] = $this->session->flashdata('password');

Good luck!
#6

[eluser]Flemming[/eluser]
@Linkark07

It's good that you got it working! But using userdata means that the student ID is stored in session for the duration of the session - is this what you really want? What happens when the professor wants to edit another student? Does it all work as you hoped?

Flashdata seemed like the right solution to me but as long as what you've got working doesn't cause you any problems then it doesn't really matter! :-)
#7

[eluser]Linkark07[/eluser]
@flemming

On the same table, I can still edit the other students.

The problem was that if I tried to enter another group list, the one I edited first still appeared and not the one I wanted. The solution was to add an unset_userdata() on the select group function.

Flashdata was good, but if the user refreshed the page then everything was gone. Really wanted to avoid that.
#8

[eluser]Flemming[/eluser]
Ahhh ok, sounds like you got the right solution then! :-)
#9

[eluser]InsiteFX[/eluser]
I use a Registry Class that I wrote for that, that stores arrays of data and also has a helper to simplify everything.

If you search the CI forums myself and another user posted our code for it.




Theme © iAndrew 2016 - Forum software by © MyBB