...

View Full Version : Losing $_POST values on refresh in a do/while loop. Store in array? How?



mathruD
07-10-2012, 08:22 AM
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 110 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:


do{
<?php
$varHolder = $row_photo_rs['photo_id'];
$var4x6 = $_POST["$varHolder-4x6"];
$var5x7 = $_POST["$varHolder-5x7"];
$var8x10 = $_POST["$varHolder-8x10"];
$var8x12 = $_POST["$varHolder-8x12"];
$var11x14 = $_POST["$varHolder-11x14"];
$var12x18 = $_POST["$varHolder-12x18"];
$var16x20 = $_POST["$varHolder-16x20"];
$var20x30 = $_POST["$varHolder-20x30"];
$varWallet = $_POST["$varHolder-wallet"];
?>
} while .......

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):


<select name="<?php echo $row_photo_rs['photo_id']; ?>-4x6">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

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.

jmj001
07-10-2012, 09:34 AM
...


do{
<?php
$varHolder = $row_photo_rs['photo_id'];
$var4x6 = $_POST["$varHolder-4x6"];
$var5x7 = $_POST["$varHolder-5x7"];
$var8x10 = $_POST["$varHolder-8x10"];
$var8x12 = $_POST["$varHolder-8x12"];
$var11x14 = $_POST["$varHolder-11x14"];
$var12x18 = $_POST["$varHolder-12x18"];
$var16x20 = $_POST["$varHolder-16x20"];
$var20x30 = $_POST["$varHolder-20x30"];
$varWallet = $_POST["$varHolder-wallet"];
?>
} while .......

...

the post vars you 'think' are there aren't really... you just can't see it because they aren't doing anything yet...

for example;


$varHolder = $row_photo_rs['photo_id'];
$var4x6 = $_POST["$varHolder-4x6"];

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...


$varHolder = $row_photo_rs['photo_id'];
$var4x6 = $_POST[$varHolder."-4x6"];

Keleth
07-10-2012, 04:04 PM
You can also do:


$var4x6 = $_POST["{$varHolder}-4x6"];

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.

mathruD
07-10-2012, 09:55 PM
You can also do:


$var4x6 = $_POST["{$varHolder}-4x6"];

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.


<?php $varOrder = ""; ?>
<?php do { ?>

<?php
$varHolder = $row_photo_rs['photo_id'];
$var4x6 = $_POST["$varHolder-4x6"];
$var5x7 = $_POST["$varHolder-5x7"];
$var8x10 = $_POST["$varHolder-8x10"];
$var8x12 = $_POST["$varHolder-8x12"];
$var11x14 = $_POST["$varHolder-11x14"];
$var12x18 = $_POST["$varHolder-12x18"];
$var16x20 = $_POST["$varHolder-16x20"];
$var20x30 = $_POST["$varHolder-20x30"];
$varWallet = $_POST["$varHolder-wallet"];
?>


<?php if(isset($_POST[$varHolder])) { ?>

<p>
<?php
echo $row_photo_rs['photo_image'];
$varOrder .= $row_photo_rs['photo_image'];
$varOrder .= "\n";
$varNullCheck = "";
?>

</p>

<ul>

<?php if ($var4x6 != 0) { ?>
<li><?php
echo "(<strong>".$var4x6."</strong>) - 4\"x6\" photos / ";
if($varNullCheck == "") {
$varOrder .= "($var4x6) - 4x6 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var4x6) - 4x6 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($var5x7 != 0) { ?>
<?php
echo "(<strong>".$var5x7."</strong>) - 5x7 photos / ";
if($varNullCheck == "") {
$varOrder .= "($var5x7) - 5x7 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var5x7) - 5x7 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($var8x10 != 0) { ?>
<?php
echo "(<strong>".$var8x10."</strong>) - 8x10 photos / ";
if($varNullCheck == "") {
$varOrder .= "($var8x10) - 8x10 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var8x10) - 8x10 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($var8x12 != 0) { ?>
<?php
echo "(<strong>".$var8x12."</strong>) - 8x12 photos / ";
if($varNullCheck == "") {
$varOrder .= "($var8x12) - 8x12 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var8x12) - 8x12 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($var11x14 != 0) { ?>
<?php
echo "(<strong>".$var11x14."</strong>) - 11x14 photos / ";
if($varNullCheck == "") {
$varOrder .= "($var11x14) - 11x14 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var11x14) - 11x14 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($var12x18 != 0) { ?>
<?php
echo "(<strong>".$var12x18."</strong>) - 12x18 photos / ";
if($varNullCheck == "") {
$varOrder .= "($var12x18) - 12x18 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var12x18) - 12x18 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($var16x20 != 0) { ?>
<?php
echo "(<strong>".$var16x20."</strong>) - 16x20 photos / ";
if($varNullCheck == "") {
$varOrder .= "($var16x20) - 16x20 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var16x20) - 16x20 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($var20x30 != 0) { ?>
<?php
echo "(<strong>".$var20x30."</strong>) - 20x30 photos / ";
if($varNullCheck == "") {
$varOrder .= "($var20x30) - 20x30 photos";
$varNullCheck = "full";
} else {
$varOrder .= "\n($var20x30) - 20x30 photos";
$varNullCheck = "full";
} ?>
<?php } ?>
<?php if ($varWallet != 0) { ?>
<?php
echo "(<strong>".$varWallet."</strong>) - wallet photos /";
if($varNullCheck == "") {
$varOrder .= "($varWallet) - wallet photos";
} else {
$varOrder .= "\n($varWallet) - wallet photos";
$varNullCheck = "full";
} ?></li>
<?php } ?>

</ul>

<?php
$varOrder .= "\n\n";
?>

<?php } ?>



<?php } while ($row_photo_rs = mysql_fetch_assoc($photo_rs)); ?>

mathruD
07-10-2012, 09:59 PM
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.


<select name="<?php echo $row_photo_rs['photo_id']; ?>-4x6">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

Keleth
07-10-2012, 10:04 PM
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

mathruD
07-10-2012, 10:11 PM
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?

Keleth
07-10-2012, 10:16 PM
Well, as I just told ya how to start a session, that plus googling "php session tutorial" should get you well under way :p

And the $_SESSION superglobal is like any other variable, just one that can be accessed on any page.

mathruD
07-10-2012, 10:45 PM
Well, as I just told ya how to start a session, that plus googling "php session tutorial" should get you well under way :p

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:


<form action="testFormTransferResults.php" method="post" name="form1">
<input name="name1" id="name1" type="text" size="32" />
<input name="name2" id="name2" type="text" size="32" />
<input name="name3" id="name3" type="text" size="32" />
<input name="name4" id="name4" type="text" size="32" />
<input name="name5" id="name5" type="text" size="32" />
<input name="Submit" type="submit" value="Submit" />
</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 for($i = 1; $i < 6; $i++) {

$_SESSION['storeName'][$i] = $_POST["name$i"];

echo "Name ".$i." is ".$_SESSION['storeName'][$i]."<br />";

} ?>

Keleth
07-10-2012, 11:01 PM
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.

mathruD
07-10-2012, 11:10 PM
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.


Here is the code for the form:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="testFormTransferResults.php" method="post" name="form1">
<input name="name1" id="name1" type="text" size="32" />
<input name="name2" id="name2" type="text" size="32" />
<input name="name3" id="name3" type="text" size="32" />
<input name="name4" id="name4" type="text" size="32" />
<input name="name5" id="name5" type="text" size="32" />
<input name="Submit" type="submit" value="Submit" />
</form>
</body>
</html>


and here is the code for processing the form and attempting to store the values in a SESSION:


<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<?php for($i = 1; $i < 6; $i++) {

$_SESSION['storeName'][$i] = $_POST["name$i"];

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.

Keleth
07-10-2012, 11:17 PM
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.

mathruD
07-10-2012, 11:30 PM
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.

mathruD
07-10-2012, 11:38 PM
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.

mathruD
07-11-2012, 12:02 AM
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.


/* 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 */

<?php
if(!isset($_SESSION['number'])) {
$_SESSION['number'] = 1;

for($i = 1; $i < 6; $i++) {
$_SESSION['storeName'][$i] = $_POST["name$i"];
}

/* 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 */

} else if(isset($_SESSION['number']) && $_POST['hiddenId'] == 1) {

for($i = 1; $i < 6; $i++) {
$_SESSION['storeName'][$i] = $_POST["name$i"];
}

}
?>

<?php for($i = 1; $i < 6; $i++) {
echo "Name ".$i." is ".$_SESSION['storeName'][$i]."<br />";
} ?>

mathruD
07-11-2012, 12:03 AM
double post

Keleth
07-11-2012, 12:27 AM
Well, the obvious thing is... don't store into the session is your POST has no values, just like you wouldn't try to send an email from a form if the form had no data submitted. If you want your session variable not to contain something blank, if $_POST isn't set, don't store new values.

The sessions are working fine, but you're not working with them properly.

mathruD
07-11-2012, 01:05 AM
Should I just do what you are saying, and check if the $_POST field is empty and then set the values into an array. That way I could avoid SESSIONS entirely?

Keleth
07-11-2012, 03:52 PM
Well, if you just store them into an array, and not a session, you won't be able to move them between pages...

I'm not really sure what you're trying to do anymore.

mathruD
07-12-2012, 08:49 PM
There are two pages (let's say page1 and page2).

The first page contains numerous photos. Each photo contains a checkbox. If that checkbox is checked, a div opens revealing multiple select menus for choosing a number of photo prints at each size. It would looks something like this when the div opened:

4"x6" [select a quanitity]
5"x7" [select a quanitity]
8"x10" [select a quanitity]
....

So the user checks of all the photos that they want, and then select a quantity of each size for those specific photos. The user then presses a submit button, which leads us to page2.

Page2 is used to process this info. All of the info from the photo form is then echoed for the user to review. So it might look something like this:

Review your order:
Photo 1 - (2) - 4"x6" prints, (4) - 5"x7" prints
Photo 2 - (3) - 4"x6" prints, (1) - 8"x10" print
........

All of that works fine. The photo selections are echoed perfectly.

Page2 also contains a simple form to take in name, email, etc. The form also contains a hidden input field to pass along the order. At this point, if the page is refreshed, or if the user attempts to send the form, but gets errors in form validation, all of the values disappear. That is because the variables were being set based on the POST values from the previous page. So, of course, if the page is refreshed for any reason, the POST values no longer exist. So I started using SESSIONS to set all of the values so that they would still show up, even on refresh. Everything is working fine now, although you told me I was using SESSIONS improperly. I'm not quite sure how I'm using them improperly, though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum