I am working on a project and basically I need to store the content of an input form in an array, but I have to add it, not replace it, this is why I use session_start().
The problem is that even after leaving the input form blank and so not submitting any input but simply refreshing the page, the last element gets added every time.
For example, let's say that for the first time I added John into the input form and I pressed the SUBMIT Button, for the first time the results will be normal, Array ([0] => John
), BUT, by simply doing refresh (and so letting the input field blank) the name John will be added into the array for no reason, this is an example of the output:
Array (
[0] => John
[1] => John
[2] => John
[3] => John
[4] => John
[5] => John
[6] => John
)
In this case, I did 6 refreshes of the page. It seems like the button remains active and thus passing the if(isset($_POST['invia_prenotazione'])) control.
<?php
session_start();
if (!isset($_SESSION['nomi']))
{
$_SESSION['nomi']=array();
}
if(isset($_POST['invia_prenotazione']))
{
$_SESSION['nomi'][]=$_POST['nome'];
echo "<br>";
print_r($_SESSION['nomi']);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Es1</title>
<style>
input
{
position: relative;
display: block;
}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Nome Evento: <input type="text" name="nome"><br>
<input type="submit" name="invia_prenotazione" value="SUBMIT"><br>
</form>
</body>
</html>