Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-10-2012, 07:22 AM   PM User | #1
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
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:

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

Code:
    <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.
mathruD is offline   Reply With Quote
Old 07-10-2012, 08:34 AM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
Quote:
Originally Posted by mathruD View Post
...

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

PHP Code:
$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...

PHP Code:
$varHolder $row_photo_rs['photo_id'];
  
$var4x6 $_POST[$varHolder."-4x6"]; 
__________________
hey... it's a sig..
jmj001 is offline   Reply With Quote
Old 07-10-2012, 03:04 PM   PM User | #3
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
You can also do:

PHP Code:
$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.
Keleth is offline   Reply With Quote
Old 07-10-2012, 08:55 PM   PM User | #4
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
Quote:
Originally Posted by Keleth View Post
You can also do:

PHP Code:
$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 Code:
<?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 is offline   Reply With Quote
Old 07-10-2012, 08:59 PM   PM User | #5
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
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.

Code:
      	<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>
mathruD is offline   Reply With Quote
Old 07-10-2012, 09:04 PM   PM User | #6
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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
Keleth is offline   Reply With Quote
Old 07-10-2012, 09:11 PM   PM User | #7
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
Quote:
Originally Posted by Keleth View Post
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?
mathruD is offline   Reply With Quote
Old 07-10-2012, 09:16 PM   PM User | #8
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 07-10-2012, 09:45 PM   PM User | #9
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
Quote:
Originally Posted by Keleth View 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:

Code:
<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 Code:
<?php for($i 1$i 6$i++) {
    
    
$_SESSION['storeName'][$i] = $_POST["name$i"];
    
    echo 
"Name ".$i." is ".$_SESSION['storeName'][$i]."<br />";

?>
mathruD is offline   Reply With Quote
Old 07-10-2012, 10:01 PM   PM User | #10
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 07-10-2012, 10:10 PM   PM User | #11
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
Quote:
Originally Posted by Keleth View Post
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:

Code:
<!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 Code:
<?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.
mathruD is offline   Reply With Quote
Old 07-10-2012, 10:17 PM   PM User | #12
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 07-10-2012, 10:30 PM   PM User | #13
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
Quote:
Originally Posted by Keleth View Post
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 is offline   Reply With Quote
Old 07-10-2012, 10:38 PM   PM User | #14
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
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 is offline   Reply With Quote
Old 07-10-2012, 11:02 PM   PM User | #15
mathruD
New Coder

 
Join Date: Apr 2011
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
mathruD is an unknown quantity at this point
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 */

<?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 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, form, loop, post, refresh

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.