add input values to array each time user hits submit

Posted on 2011-10-06
Last Modified: 2012-05-12
I am trying to create a temporary list to display before it submits to my DB.

I want to create an array of values the user is submitting, and have that displayed when the user hits submit, and have each subsequent value added as the user inputs and sends.

I have it working to update and display, but I can't figure out how to get it to keep the value and add new.

any suggestions?
<fieldset><legend>Enter Contributor</legend>
<form action="" method="post">
<input type="input" name="name"> Name <input name="submit" type="submit">

<?php if(isset($_POST['submit'])) {
	$_SESSION['names'] = $_POST['name'];
} ?>

<?php echo $_SESSION['names']; ?>

Open in new window

Question by:axessJosh
    LVL 10

    Expert Comment

    When using $_SESSION you MUST call session_start() at the beginning of your script before ANY output is sent to the browser (this includes whitespace). Otherwise the session array will not be kept from page load to page load.
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    If you don't have a huge amount of data, you could use the $_SESSION variable.  The drawback of this is that the information will be lost if the user closes his browser.

    One session variable could be
    $_SESSION [ "NameCount" ]   Start it at zero and just add one each time a new value is added.

    While I haven't tried this, I think you can use an integer as an index

    $_SESSION [ 0 ] = $_POST["name"];
    $_SESSION [ 1 ] = $_POST["name"];
    and so forth.

    However I'd try something like $_SESSION [ $_SESSION [ "NameCount" ]] = $_POST["name"];
    $_SESSION [ "NameCount" ]++;   // I think that will work.  In any event, add 1 to the value.

    Or you could do
    $_SESSION [ "Name_" . $_SESSION [ "NameCount" ]] =
    if you want to make it clearer what is in the array.

    Hope this makes sense.  I should be sleeping...
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    Derokorian is right about needing to call session_start()

    My focus was on program design, if I were doing this.  Hope I got it right.  Anyway, g'nite.
    LVL 2

    Author Comment


    i used the code you sent which seems to work correctly top update the count, however, what I'd like is to add the value of the input.  For instance, the user inputs the name "jeff".  then the region below displays Jeff.  The user can then add another name, say "steve".  Now the region displays:


    as a third name is input, it would be added sequentially.

    Make sense?
    LVL 9

    Accepted Solution

    Just store the array of names in the session, then to display, loop over the array with foreach()

      $_SESSION['names'][] = $_POST['name'];
    foreach ($_SESSION['names'] as $name) 
      echo $name .'<br>';

    Open in new window

    When you're done with using the list, make sure you use
    $_SESSION['names'] = array();

    Open in new window

    in case the user comes back to the page during the same session and expects the array to be empty.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now