1

I have a task to take input from user and push it in array. How can I take input from input field submit it, push it in to array and then do this again?

    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        Input Stuff in this Table :
        <input type="text" name="insert">
        <input type="submit" name="submit">
    </form>

    <?php
        $insert = ($_POST["insert"]);
        array_push($data, $insert);
        print_r($data);
    ?>
blackbishop
  • 30,945
  • 11
  • 55
  • 76
Samurai
  • 55
  • 7
  • Does this answer your question? [jQuery Ajax POST example with PHP](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Funonly Dec 28 '21 at 10:51

1 Answers1

2

you will miss the content of your variable and will re-initialize it on every page load, the solution is to keep your $data variable in the session and use it on every form submission:

<?php
   session_start();
   $data = isset($_SESSION['data']) ? $_SESSION['data'] : []; 
   array_push($data, $_POST["insert"]);
   $_SESSION['data'] = $data;
?>
behzad m salehi
  • 1,038
  • 9
  • 23