doubledee — 2012-09-03T09:46:28-04:00 — #1
If I gather data from my database, stick it into an Array, use that array to populate a Form, and then the Form is submitted, what happens to the data in my Array?
I know that $_POST persists across a Form being submitted, and I think that the data in my hypothetical Array above would be lost, but I'm not sure... :-/
tpunt — 2012-09-03T11:15:05-04:00 — #2
Once the page has finished loading, PHP's garbage collection will come along and free up any allocated resources (from actions that have finished), which includes deleting your array data. So yes, your array data will be lost, but also saved into the POST data (upon form submission), which can be access through PHP's superglobal $_POST.
serverstorm — 2012-09-03T11:47:53-04:00 — #3
As a side note, if you need Arrays to persist across pages not using $_POST then serialize them and insert that serialized array into the database, then unseralize it on another page where you need it.
qmonkey — 2012-09-05T16:48:30-04:00 — #4
Read up on "variable scope". But for your question, you should realize that by the time the user sees the web page, PHP is done (not always but ignore output buffering for now). So the PHP makes some HTML and sends it to the user like mailing a letter. The user can send you a letter back by submitting the form. The letter your PHP gets back will have the $_POST variable filled. This letter is written in the user's handwriting and is completely separate from the one you sent.
We have sessions (and other methods) if you need to save some info from the letter you wrote and compare it to the one you get back but otherwise any thoughts you had while writing your letter (the variables you used) are gone and forgotten. Once you send your letter your mind is erased!
force — 2012-09-05T21:50:54-04:00 — #5
$_SESSION persists across page loads.
doubledee — 2012-09-05T22:08:11-04:00 — #6
I am having a larger issue, and thought using an array might help.
It is probably better to stick with the $_POST array since that is what it is designed for, i.e. to pass data between scripts after a Form is submitted.
force — 2012-09-05T23:47:37-04:00 — #7
$POST and $GET are designed to send data from client to server. $_SESSION is designed for non-permanent, but persistent storage on the server-side during multiple page requests.