0

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>
guyaloni
  • 4,972
  • 5
  • 52
  • 92
Jasper Howard
  • 167
  • 1
  • 7
  • 1
    You just need to redirect your page after submitting the form. This is why it's a good idea to separate your HTML and PHP into their own files, because you would be forced to redirect after your PHP script ends, which would solve your problem. – GrumpyCrouton Nov 05 '20 at 17:39
  • @GrumpyCrouton I do agree that this is a great practice, but because this is a homeworkd, my teacher said to keep everything in one file – Jasper Howard Nov 05 '20 at 17:41
  • Beside the best practices... A quick fix would be to unset the POST variable after `$_SESSION['nomi'][]=$_POST['nome'];` --- Like: `unset($_POST['nome'])` -- [PHP manual](https://www.php.net/manual/en/function.unset.php) – Louys Patrice Bessette Nov 05 '20 at 17:42
  • @LouysPatriceBessette I tried and for some reason it didn't work, it's unusual why that would be. But thanks anyways, I know it should work but for some reason it doesn't. – Jasper Howard Nov 05 '20 at 17:50
  • @JasperHoward Just redirect at the end of your PHP back to the same page. – GrumpyCrouton Nov 05 '20 at 18:17

1 Answers1

1

Once you perform submit, any time you refresh your page the same action is being taken again, which means you submit your form again.

have a look here:
Best way to avoid the submit due to a refresh of the page

Generally, redirecting a form page to the same page is not a good practice, neither include logic in your view file. Instead, you should have a login php file and a view file.

guyaloni
  • 4,972
  • 5
  • 52
  • 92
  • That fixed it, I know that redirecting to the same page is not good practice, but due to the fact that is is a homework, my teacher said that I should do it in one file only. Thanks for the help! – Jasper Howard Nov 05 '20 at 17:50