Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:30:06+00:00 2026-06-17T11:30:06+00:00

I am trying to take input from a form, add it to an array,

  • 0

I am trying to take input from a form, add it to an array, and print_r that array to the screen.

My problem is that the input from the form only replaces the first (and only) element in the array.

<form action="" method="POST">
    <input type="text" name="text" />
    <input type="submit" name="sub"/>
</form>

<?php
    $a = array();

    if( isset($_REQUEST['text']) && !empty($_REQUEST['text'])){
        array_push($a, $_REQUEST['text']);
        print_r($a);
    }
?>

One theory of mine is that $a keeps getting re-assigned on the first line of PHP code ($a = array();), but I’m not sure how to fix it. I have looked around, but can’t find an answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:30:06+00:00Added an answer on June 17, 2026 at 11:30 am

    You are correct. The array does get reinitialized each time the form is posted. What you’ll want to do is have your array as a more persistent data source.

    1. You might consider using a session and the $_SESSION variable.
    session_start();
    if (!is_array($_SESSION['a'])){
      $_SESSION['a'] = array();
    }
    $_SESSION['a'][] = $_REQUEST['text'];
    
    1. You might also consider writing this data to a small text file that you could then read at the start of the script.

    2. Another option would be to write the data to a $_COOKIE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a method that take 2 int array as the input
I'm trying to take user form input and display it back to the user,
I'm trying to write my first Android app. It will take a number input
I've been trying to create some php code that would let me take input
I'm trying to take my Form layout away from tables and entering the world
I am trying to take a single input from the user and delete some
I have a data in form a dictionary.. NOw I take the input from
I am trying to take user input and convert it in the form of
I am trying to set up a php script that takes the input from
I'm trying to make a calculator that will take inputs from users and estimate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.