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;
}
}
?>