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!
Add item to an array
Moderator: General Moderators
Re: Add item to an array
Try using a session to store your array to each time.
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: Add item to an array
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.
Yeah, what the above poster said.. lol. I didn't see that he had posted already.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Add item to an array
Not that is matters, $_SESSION is already an arraySkoalbasher wrote:You can turn the $_SESSION variable into an array
Re: Add item to an array
Thanks everyone for the help. Sessions were exactly what I was looking for and solved my problem nicely.
Re: Add item to an array
Just a note that cookies are almost certainly required in order for $_SESSION to work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Add item to an array
Unless of course they have session.use_trans_sid enabledpickle wrote:Just a note that cookies are almost certainly required in order for $_SESSION to work.