PHP Save form data in array and display on another page

Asked

Viewed 1,235 times

2

Good Afternoon.

I am trying to create a form whose data will be stored in an array and should be displayed on another page when the people’s registration is finalized. I’m trying to do this with SESSION, however, I believe I’m not being able to pass the data from the form to the vector. No data is displayed on the display page.

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(display registered persons):

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

Does anyone know what it can be?

  • You are starting the session where you store the data in $_SESSION ?

  • Yes. On both pages I am declaring "session_start();"

  • Do a test like this: var_dump($_REQUEST) before trying to save in the $_SESSION array and check if you are receiving the data, and if you received it, then try: $_SESSION['register']['name'] = $_REQUEST['name'] and see if this field has this value on page 2

  • This var_dump test was on the 'cadastros.php' page with var_dump($_REQUEST), is that it ? NOTE: the file that Open in the session, is the same registration name ? with capital S ?

  • I did the checks and, with the commands you passed and now is displaying the data on page 2, but displays only the last data entry. I tried using push($_SESSION['registration'], $_REQUEST['name'], ... $_REQUEST["email"]); to add new entries, but only the last one returns.

  • You want it to store a different session every time you submit the form and store all that are sent?

  • All registered must be displayed at once after.

Show 2 more comments

1 answer

3


Assuming you’ve already sent the form and received the information on the other side:

Log in again:

session_start();

And just below do a light work with the arrays so you can 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 functional example on Ideone

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.