Missing array elements when changing pages? [duplicate]

0

I'm learning PHP and am trying to insert values into a array through a input , write a value in input and click the Add Array button and go to the PHP file where it has the function to add the written value in the created array, but it only inserts the last value, if I try to insert 5 numbers, when I ask to show it will only have the last number inserted, the first 4 will have been lost, because?

HTML:

<?php
include_once 'includes.php';
?>

<!DOCTYPE html>
<html>
<head>
    <title>Add Array</title>
</head>
<body>

    <h2>Add</h2>
    <hr>
    <div style="width: 50%; margin-left: 30px;">
        <div class="form-group" style="width: 300px;">
            <form method="POST" action="function/add.php?action=addArray">
                <label for="valor">Valor:</label>
                    <input type="text" class="form-control" name="valor">
                </div>
                <button class="btn btn-info">Add Array</button>
            </form>   
    <br>
    <br>
    </div>
</body>
</html>

PHP:

<?php

if(!empty($_GET['action']))
{
    switch ($_GET['action']) 
    {
        case 'addArray':
            $cesta = array(); //Cria variavel tipo array.
            $valorInserir = $_POST['valor']; //Atribui valor pra variavel
            array_push($cesta, $valorInserir); //Insere no array CESTA o valor
            header("Location: ../index.php"); //Direciona pra index novamente
            break;

        default:
            break;
    }
}

?>
    
asked by anonymous 12.09.2017 / 16:17

4 answers

1

That's exactly what all the answers in this post say.

In your case, just do this:

session_start();

if(!empty($_GET['action'])){

    switch ($_GET['action']) 
    {
        case 'addArray':

            $valorInserir = $_POST['valor']; //Atribui valor pra variavel
            //armazenando os valores anteriores concatenados com o novo valor usando separador virgula
            $_SESSION['cesta'] = $_SESSION['cesta'].",".$valorInserir;

            //header("Location: ../index.php"); //Direciona pra index novamente

            break;

        default:
            break;
    }

    $meuArray = explode(',', $_SESSION['cesta']);
    print_r($meuArray);
}
  

Sessions are temporary files that hold information on the server. What are worth for? It has the same functionality as the famous COOKIE, but the advantage is that the client computer does not have to be enabled to use it.

     

To start a session, we use the session_start function

    
12.09.2017 / 17:01
0

Each time you submit the form, php will run again, meaning all variables are re-created. You must store the previous values somewhere, for example, create a variable in the session and each time the value is sent, update the variable. You have to create the session before storing any value in it. See php documentation about sessions.

    
12.09.2017 / 16:33
0

Every time you make a new request to the server it interprets PHP causing the variable to return to the default value in case $cesta = array()

You could save this value in cookies or session.

link

link

    
12.09.2017 / 16:42
0

When you enter a number and click "Add Array", that number will be "pushed" in the $cesta array. However, after that you redirect to index.php, when you redirect, you lose what was inside $cesta , if you want to persist everything you send to $cesta you have to use a database or even sessions.

Taking this into account, here is an example of how you could do this:

HTML (here will not change anything)

<?php
include_once 'update.php';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Add Array</title>
</head>
<body>

    <h2>Add</h2>
    <hr>
    <div style="width: 50%; margin-left: 30px;">
        <div class="form-group" style="width: 300px;">
            <form method="POST" action="function/add.php?action=addArray">
                <label for="valor">Valor:</label>
                    <input type="text" class="form-control" name="valor">
                </div>
                <button class="btn btn-info">Add Array</button>
            </form>   
    <br>
    <br>
    </div>
</body>
</html>

PHP

<?php

session_start();
if(!empty($_GET['action']))
{
    switch ($_GET['action']) 
    {
        case 'addArray':
        $cesta = array(); //Cria variavel tipo array.
        $valorInserir = $_POST['valor']; //Atribui valor pra variavel
        array_push($cesta, $valorInserir); //Insere no array CESTA o valor
        $_SESSION['valor'][] = $cesta;
        header("Location: ../index.php"); //Direciona pra index novamente
            break;

        default:
            break;
    }
}
echo "<pre>"; echo print_r($_SESSION['valor']) ; echo "</pre>";
    
12.09.2017 / 16:35