PHP Save form data to array and display in another page

2

Good afternoon.

I'm trying to create a form whose data will be stored in an array and should be displayed on another page when people's records are finalized. I'm trying to do this with SESSION, however, I do not think I'm getting the shape data into the vector. The data page does not display the data.

My Page 1:

<?php
    session_start();
    $aluno =  array();
    $_SESSION['cadastro'] = $aluno;
?>

<form action="cadastroS.php"method="POST">
    <input type="text" name="nome" placeholder="Nome Completo"></br>
    <input type="number" name="ra" placeholder="RA"></br>
    <select name="gender">
       <option value="Masculino">
       <option value="Feminino">
       <option value="Outro">
    </select></br>
    <input type="number" name="idade"  min="1" max="99"  placeholder="Idade"></br>
    <input type="text" name="endereco" placeholder="Endereço"></br>
    <input type="tel" name="telefone" placeholder="Telefone"></br>
    <input type="email" name="email"    placeholder="email"></br></br>
    <input type="submit" name="cadastrar" value="Cadastrar">
</form>

  <?php
    if(!isset($_SESSION['cadastro'])){
        array_push(
              $_SESSION['cadastro'],  
              $_REQUEST['nome'],
              $_REQUEST["ra"],
              $_REQUEST["gender"],
              $_REQUEST["idade"],
              $_REQUEST["endereco"],
              $_REQUEST["telefone"],
              $_REQUEST["email"]
        );
    }
  ?>

page 2 (view people registered):

<?php
session_start();
    foreach ($_SESSION['cadastro'] as $key => $value) {
        echo $key .':' . $value, '<br>';
    }
?>

Does anyone know what it can be?

    
asked by anonymous 01.11.2017 / 16:04

1 answer

3

Assuming you have submitted the form and received the information on the other side:

Log in again:

session_start();

And just below do a little work with the arrays to be able to keep storing the information:

$_SESSION['cadastro'] = [];
$cads = count($_SESSION['cadastro']);

if($cads == 0){
    $_SESSION['cadastro'][0] = $_REQUEST;
}else{
    $new_cad = $cads + 1;
    $_SESSION['cadastro'][$new_cad] = $_REQUEST;
}

See a working example at Ideone

And to display on page 2, a small change is required because $key will no longer be the field name:

foreach ($_SESSION['cadastro'] as $key => $values) {
    echo $key . ':';
    foreach($values as $key_value => $value){
         echo $key_$value . ':' . $value . '<br>';
    }
}
    
01.11.2017 / 17:29