[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


add input values to array each time user hits submit

Posted on 2011-10-06
Medium Priority
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

ID: 36928669
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
ID: 36928673
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
ID: 36928675
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.

Author Comment

ID: 36928703

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?

Accepted Solution

ghodder earned 2000 total points
ID: 36928801
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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