Page 1 of 1

Add item to an array

Posted: Tue Feb 10, 2009 4:20 pm
by jabaro
I'm fairly new to PHP, and am having trouble adding items to an array from an html form.

What I'm trying to do is allow my users to search for an item, and when they find that item, have it POST to the same page, and add that item to an array. I also want to be able to do this many times to add multiple items to the array. I can currently add one item to the array with using POST, but when I try adding subsequent items it overwrites my array each time the page loads and thus erases any previous item.

Is there a way to do this with just PHP or will I need to use a database or javaScript to facilitate this? Thanks in advance for the help!

Re: Add item to an array

Posted: Tue Feb 10, 2009 4:28 pm
by mbdigital
Try using a session to store your array to each time.

Re: Add item to an array

Posted: Tue Feb 10, 2009 4:37 pm
by Skoalbasher
You can turn the $_SESSION variable into an array. That stays until the session times out. Would be exactly what you're looking for I believe.

Yeah, what the above poster said.. lol. I didn't see that he had posted already.

Re: Add item to an array

Posted: Tue Feb 10, 2009 7:00 pm
by John Cartwright
Skoalbasher wrote:You can turn the $_SESSION variable into an array
Not that is matters, $_SESSION is already an array :wink:

Re: Add item to an array

Posted: Wed Feb 11, 2009 12:03 am
by jabaro
Thanks everyone for the help. Sessions were exactly what I was looking for and solved my problem nicely.

Re: Add item to an array

Posted: Wed Feb 11, 2009 10:41 am
by pickle
Just a note that cookies are almost certainly required in order for $_SESSION to work.

Re: Add item to an array

Posted: Wed Feb 11, 2009 11:14 am
by John Cartwright
pickle wrote:Just a note that cookies are almost certainly required in order for $_SESSION to work.
Unless of course they have session.use_trans_sid enabled