Losing $_POST values on refresh in a do/while loop. Store in array? How?
I'm having a problem retaining $_POST variables on refresh. I have a page that contains numerous photos, each with a checkbox. When the checkbox is checked, a div opens with various sizes for photo prints. There is a select menu that accompanies each size print, and the user can select 1–10 prints. The user selects the desired photos and number of prints at each size, and submits the form.
Then, a do/while loop is used to output all of the selected prints/quantities (think of this as a "review your selections" type of page). This works perfectly using the following code:
To give you a better idea of what is going on there, this is an example of an input field for a photo (this is part of the form that is passed over from the previous page):
Notice that that the "name" field for the select box is the photo_id, and then "-4x6." This same thing happens for the other various sizes (as you'll notice in the variable definitions above). So the loop goes through the photo_id's and determines how many of each photo is ordered at each size. Once the loop finishes with the first photo_id, it loops to the next one, resetting the value for $varHolder, and all of the other variables, in the process. This, in turn, changes the quantity of each size for that specific photo.
Again, this all works fine, but if a refresh happens, it's all lost. How do I retain everything in the output even during refresh? It's not part of a form, but simply output to the screen. I've looked at some similar questions, and it seems like maybe an array of some type is the answer, but I couldn't make sense of what to do in relation to my exact problem. Would it be a multi-dimensional array? Any help would be greatly appreciated.
there is no var $varHolder-4x6 that I can see, unless it's somewhere else in your code but my assumption from what you have said is that you expect '-4x6' is being appended to $varHolder but this is not what's happening
to append it you will need to concatenate the values like so...
I know sometimes concatenation loses the flow of the line. But yah, always remember, if a variable isn't working as intended, echo it out, echo out the variable name as a string, etc, to make sure you know what you're doing.
I know sometimes concatenation loses the flow of the line. But yah, always remember, if a variable isn't working as intended, echo it out, echo out the variable name as a string, etc, to make sure you know what you're doing.
Sorry, I think I may have confused you by not putting the code that contains my variables. I didn't think I would need to show it in this case. I will post the entire code below. The variables are being set fine. They also echo perfectly. The problem is when I refresh the page. That's when all the variables that have been echoed disappear. That's where I'm thinking I need some kind of SESSION array or something to hold everything. I'm just confused about how to do that properly with the variables resetting every time the loop takes on a new photo_id. Also, $varOrder is simply a variable to store all of the info and pass it in an email. That works fine so pay no attention to it.
Also, the variables such as $varHolder-4x6 are being pulled from the form that is being passed over. I didn't include that code, either. Again, sorry about that. I didn't think I would need it for this specific question. Here is an example of a select box on the previous page, in which $varHolder-4x6 is being submitted. In this case, it's pulling photo_id-4x6, but you'll notice in the processing code the $varHolder is set equal to the photo_id, thus $varHolder-4x6. This same process goes on for all of the select boxes for the various sizes of photos.
Oh, you mean a literal refresh... yah, you'll lose your POST data (some browsers will ask if you want to resend data if you're on a page that sent POST data to itelf). Throw a session_start() as your first line, then you can store items into the $_SESSION superglobal
Oh, you mean a literal refresh... yah, you'll lose your POST data (some browsers will ask if you want to resend data if you're on a page that sent POST data to itelf). Throw a session_start() as your first line, then you can store items into the $_SESSION superglobal
Yes, the POST data is being lost. I'm unsure of how to use a SESSION to retain everything. Does it need to go in an array? How should I go about storing it in the SESSION? Also, do I echo it back out of the SESSION instead of the POST?
Well, as I just told ya how to start a session, that plus googling "php session tutorial" should get you well under way
And the $_SESSION superglobal is like any other variable, just one that can be accessed on any page.
I've tried google for days now. I'm just a bit confused as to exactly what it is I'm doing. I tried a simple test to see if I'm doing it right, but I must not be, as I lost everything on refresh again. I made a simple form:
and then sent it to a page with the following code, in which i tried to set a SESSION, but it isn't working (i included session_start() at the top of the page even though it isn't shown here). i have no clue what i'm doing wrong. it echoes fine the first time, but i still lose everything on refresh. Is this even on the right track?
PHP Code:
<?php for($i = 1; $i < 6; $i++) {
$_SESSION['storeName'][$i] = $_POST["name$i"];
echo "Name ".$i." is ".$_SESSION['storeName'][$i]."<br />";
Well, your use is dead on, so you're on the right track.
You're positive that session_start() is the first line of your code right? Above the HTML, above everything else. The only thing acceptable above it is the PHP open tag
If you have all that, show your full code with the session code actually in there. Remember, if you don't show us, we can only assume you don't know/didn't try.
Well, your use is dead on, so you're on the right track.
You're positive that session_start() is the first line of your code right? Above the HTML, above everything else. The only thing acceptable above it is the PHP open tag
If you have all that, show your full code with the session code actually in there. Remember, if you don't show us, we can only assume you don't know/didn't try.
echo "Name ".$i." is ".$_SESSION['storeName'][$i]."<br />";
} ?>
</body> </html>
It's echoing out the SESSION variables just fine the first time, but refresh causes everything to be lost again. Thanks for continuing to help with this.
Well, of course on a refresh that second page will fail.... you're searching for a number of POST variables that don't exist, and thus storing nothing into a bunch of session variables you had previously put values into, overwriting them with blank (I forget if its NULL or an empty array).
Think about it... the first time you're there, that code stores POST values that exist into a session. When you refresh, and the POST values no longer exist, you're STILL trying to store values into the SESSION, which is the same as if you had tried to echo the POST variables themselves.
Well, of course on a refresh that second page will fail.... you're searching for a number of POST variables that don't exist, and thus storing nothing into a bunch of session variables you had previously put values into, overwriting them with blank (I forget if its NULL or an empty array).
Think about it... the first time you're there, that code stores POST values that exist into a session. When you refresh, and the POST values no longer exist, you're STILL trying to store values into the SESSION, which is the same as if you had tried to echo the POST variables themselves.
Yes, I do understand this. That's the question I was trying to get across in my original post, although it may have been a bit confusing. I thought SESSIONS would solve the problem. What do I have to do to keep the variables echoed to the page even after refreshing? I figured they were overwriting as blank, but I have no clue how to prevent that from happening on refresh.
I guess I should also ask if there is any way to write some type of if-statement that only sets the variables when the page is accessed by submitting the form. That way refresh wouldn't reset the variables. I don't even know if that is possible.
Ok. I think I have solved my problem. I used two if-statements to determine if the $_POST variables needed to be reset or not.
PHP Code:
/* this if-statement checks to see if the SESSION variable has been set. if not, it sets the SESSION variable and stores all of the POST values into the SESSION. this retains the values, even on refresh, since the variables don't try to reset again on refresh due to the SESSION variable being set */
/* the next if-statement only resets the variables if the SESSION variable is set, and the form passes over a hidden field with hiddenId = 1. without the additional check for the hiddenId, the variables would never reset, even when a new the form was submitted with different values */