Link to home
Start Free TrialLog in
Avatar of axessJosh
axessJosh

asked on

add input values to array each time user hits submit

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">
</form>
</fieldset>

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

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

Open in new window

Avatar of Derokorian
Derokorian
Flag of United States of America image

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.
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...
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.
Avatar of axessJosh
axessJosh

ASKER

hmccurdy,

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:

Steve
Jeff

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

Make sense?
ASKER CERTIFIED SOLUTION
Avatar of ghodder
ghodder
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial