Losing array elements when switching pages?

Asked

Viewed 296 times

0

I am learning PHP and am trying to insert values in a array through a input, write a value on input and I click on the Add Array button and go to the PHP file where it has the function to add the value written in the created array, but it only inserts the last value, if I try to insert 5 numbers, when I ask to show 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;
    }
}

?>

4 answers

1


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

In your case just do so:

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 store information on the server. What are they for? It has the same functionality as the famous COOKIE, but the advantage is that the client’s computer does not need to be enabled to use it

To start a Session, we use the function session_start

0

Each time you submit the form, php will run again, that is, all variables are created again. You should 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.

0

When you place a number and click on "Add Array", that number will be "pushed" in the array $cesta. 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 inside $cesta you have to use a database or even sessions.

Taking this into consideration, here’s an example of how you could do it:

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>";

0

Browser other questions tagged

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