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);
    ?>
1

1 Answer 1

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;
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.