Session PHP does not work

0

I'm starting with PHP and when trying to do a proposed exercise in a workbook, my $ _Session variable is not working as it should, can you tell me what the error in the code is?

<?php session_start() ?>

<html>
    <head>
        <title>Contatos</title>
    </head>
    <body>
        <h1>Contatos</h1>
        <form>
            <fieldset>
                <legend>Adicionar novo contato</legend>
                <label>
                    Nome: <input type="text" name="nome" />
                </label>
                <label>
                    Telefone: <input type="text" name="telefone" />
                </label>
                <label>
                    Email: <input type="text" name="email" />
                </label>
                <input type="submit" value="Salvar Contato" />
            </fieldset>
        </form>

    <?php

        $nome = array();
        $telefone = array();
        $email = array();

        if (isset($_GET["nome"]))
        {
            $_SESSION["a"] = $_GET["nome"];
            $_SESSION["b"] = $_GET["email"];
            $_SESSION["c"]  = $_GET["telefone"];                    
            $nome[] = $_SESSION["a"];
            $email[] = $_SESSION["b"];
            $telefone[] = $_SESSION["c"];
        }

    ?>
    <table>
    <tr>
        <th>Nome</th>
        <th>Telefone</th>
        <th>E-mail</th>
    </tr>
    <tr>
    <?php foreach ($nome as $nome) : ?>
            <td><?php echo $nome ?></td>
    <?php endforeach; ?>
    <?php foreach ($telefone as $telefone) : ?>
            <td><?php echo $telefone ?></td>

    <?php endforeach; ?>
    <?php foreach ($email as $email) : ?>
        <td><?php echo $email ?></td>
    <?php endforeach; ?>
    </tr>
    </table>
</body>
</html>
    
asked by anonymous 11.07.2016 / 16:41

1 answer

1

Change this snippet:

        $_SESSION["a"] = $_GET["nome"];
        $_SESSION["b"] = $_GET["email"];
        $_SESSION["c"]  = $_GET["telefone"];                    
        $nome[] = $_SESSION["a"];
        $email[] = $_SESSION["b"];
        $telefone[] = $_SESSION["c"];

To:

        $_SESSION["nome"] = $_GET["nome"];
        $_SESSION["email"] = $_GET["email"];
        $_SESSION["telefone"]  = $_GET["telefone"];                    

So we can pass the variables into the session already

    
11.07.2016 / 16:45