Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

values is incrementing everytime page is refreshed

Status
Not open for further replies.

misslois74

Programmer
Sep 27, 2008
63
0
0
PH
im working on a page in which im using an array stored in a session now my problem is everytime i click on F5 or refresh the page the value that is stored on that session is increasing eventhough i didnt click on submit....
here is my sample code:

Code:
if (!isset($_SESSION['cart']))
									{
										$_SESSION['cart'] = array();
									}
									
									$quer = "Select * from tbl_candle";
									$result = mysql_query($quer);
									while($row = mysql_fetch_array($result))
									{
										$sku = trim($row[product_id]);
										$CATALOG[$sku][prod_id] = trim($row[product_id]);
										$CATALOG[$sku][prod_code] = trim($row[product_code]);
										$CATALOG[$sku][prod_name] = trim($row[product_name]);
										$CATALOG[$sku][prod_desc] = trim($row[product_description]);
										$CATALOG[$sku][prod_price] = trim($row[product_price]);
										$CATALOG[$sku][prod_image] = trim($row[product_image]);
									}
									
									/*if this is an add operation
									  add to already existing quantities in shopping cart*/
									
									if (isset($_GET['delete']))
									{
										

										unset($_SESSION['cart'][$_GET['delete']]);
										//unset($myShopping['order'][$_GET['delete']]);
									}
									
									if (isset($_POST['add']))
									{
									  
											foreach ($_POST['a_qty'] as $k => $v)
										   {
												if($v != 0)
												{
														$_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v;
												}
											}
									  //}
									}
									
									/* if this is an update operation
									   replace the quantities in shopping cart with values entered*/
									if($_POST['update'])
									{
										if(isset($_POST['u_qty']))
										{
											foreach($_POST['u_qty'] as $k => $v)
											{
												//if the value is empty, 0 or negative
												//dont bother changing the cart
												if($v != "" && $v >= 0)
												{
													$_SESSION['cart'][$k] = $v;
												}
											}
										 }
										 
									}
									
																	
									
									if($_POST['process'])
									{
										$date = date('Y-m-d');
										$g_total = $_SESSION['grand_total'];
										$custid = $_SESSION['c_id'];
										mysql_query("Insert into tbl_order(date_order,customer_id, total_amount) values('$date','$custid','$g_total')") or die(mysql_error());
										$ordid = mysql_insert_id();
										if(isset($_SESSION['cart']) && is_array($_SESSION['cart']))
										{
											foreach($_SESSION['cart'] as $k => $v)
											{
												$prodcode = $CATALOG[$k][prod_code];
												$prodname = $CATALOG[$k][prod_name];
												$prodprice = $CATALOG[$k][prod_price];
												$qty = $v;
												$total = $qty * $prodprice;
												mysql_query("Insert into tbl_order_details(order_id, product_code, product_name, product_price, quantity, sub_total) values('$ordid','$prodcode','$prodname','$prodprice','$qty','$total')") or die(mysql_error());
											}
										}
									 }
												
								?>
                                        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
																<p><br>
																Enter the quantity of the product you wish to order.</p>
																<table width="650" border="0">
  <tr>
    <td width="142">PRODUCT CODE</td>
    <td width="187"><div align="center">PRODUCT NAME</div></td>
    <td width="121"><div align="center">IMAGE</div></td>
    <td width="74"><div align="center">PRICE</div></td>
    <td width="114"><div align="center">QUANTITY</div></td>
  </tr>
</table>

										  <table width="655" border="0">
								<?php
									//print items from the catalog for selection
								if(is_array($CATALOG))
								{
									foreach($CATALOG as $k => $v)
									{  ?>
										<tr><td width="176"><b><?php echo $v[prod_code]; ?></b></td>
										<td width="173"><b><?php echo $v[prod_name]; ?></b></td>
										<td width="125"><a href="<?php echo $v[prod_image]; ?>" onclick="window.open(this.href, 'child', 'height=200, width=250'); return false"><img src="<?php echo $v[prod_image]; ?>" width="74" height="69" title="CLICK TO ENLARGE"></a></td>
										<td width="70"><b><?php echo $CATALOG[$k][prod_price]; ?></b></td>
										<td width="104"><input size=4 type=text name="a_qty[<?php echo $k; ?>]"></td></tr>
										
							  <?php }
								}  ?>
								
								<tr>
								<td colspan="5"><br />
								<center><input type="submit" name="add" value="Add items to order" title="Click to Order"></center>
								</td>
								</tr>
								</table>
                                
                                <br>
                                        
						<h2>Order List</h2>
								<table width="36%" border="0" cellspacing="0">
								<?php
								  //initialize a variable to hold total cost
								  $total = 0;
								  $subtotal = 0;
								  
								  //check the shopping cart
								  //if it contains values
								  //look up the SKUs in the $CATALOG array
								  //get the cost and calculate subtotals and totals
								  
								 								  
								  if(is_array($_SESSION['cart']) || isset($_SESSION['cart']))
								  { 
								  	 ?>
                                    
									<tr style="font-size:10px"><td colspan="2">Date: <?php echo date('Y-m-d'); ?></td></tr>
							<?php   //foreach($_SESSION['cart'] as $k => $v)
									foreach($_SESSION['cart'] as $k => $v)
									{
										//only display items that have been selected
										//that is, quantities > 0
										$qty = $v;
										if ($qty > 0)
										{  
											$subtotal = $qty * $CATALOG[$k][prod_price];
											$total += $subtotal;  
											$_SESSION['grand_total'] = $total; ?>
											<tr style="font-size:10px"><td colspan="2"><b><?php echo $CATALOG[$k][prod_name]; ?></b></td></tr>
											<tr style="font-size:10px"><td>Php<?php echo $CATALOG[$k][prod_price]; ?>&nbsp;&nbsp;&nbsp;x&nbsp;&nbsp;&nbsp;<?php echo $qty; ?></td></tr>
											<tr style="font-size:10px"><td>New quantity:</td><td><input size=4 type="text" name="u_qty[<?php echo $k; ?>]" value="<?php //echo $v; ?>"></td></tr>
											<tr style="font-size:10px"><td>Sub-total:</td><td>Php<?php echo sprintf("%0.2f", $subtotal); ?></td></tr>
											<tr style="font-size:10px">
											  <td colspan="2"><a href="list_order.php?delete=<?php echo $CATALOG[$k][prod_id]; ?>"><b>REMOVE ORDER</b></a></td>
											</tr>
											<tr>
											  <td colspan="2"><hr size="2" color="#3333CC" /></td>
											</tr>
											
											
									<?php } ?>
								<?php }  ?>
								<tr style="font-size:14px">
								<td width="68%"><b>Total: </b></td>
								<td width="8%"><b>Php <?php echo sprintf("%0.2f", $total) ?></b></td>
								</tr>
								<tr>
								<td><input type="submit" name="update" value="Update" size="20"></td><td><input type="submit" name="process" value="Process" size="20"></td>								  <!--<input type="submit" name="clear" value="Clear Order">-->
								</tr>
								</table>
								</form>
							<?php } ?>
                                     <br /><br />
 
i click on F5 or refresh the page the value that is stored on that session is increasing eventhough

I'm assuming you are getting, the famous
"To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier."
Or for IE:

To display the web[page again, Internet Explorer needs to resend the information you've previously submitted...

that means that you are in fact re-submitting the form each time you refresh the page.

Pressing the button is irrelevant. The form gets submitted and all actions are performed again.

But of course this is expected behavior, since you are "refreshing the page" you want the actions to occur again.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks for the response so what do i need to do to be able to solve my problem in case the user presses F5 any suggestion...
 
You would have to get a bit creative. A user refreshing the page is the same action as a user adding the product to the cart in the first place. Off the top of my mind, I would try creating a unique value that is passed along with the product information and storing this in the session. If this unique value matches one already in the session, don't add it. You would have to generate this unique value every time a user goes to a product page and add it to a hidden field.

Scott Prelewicz
Web Developer
COMAND Solutions
 
You need to have some kind of mechanism that checks whether the form has already been submitted.

Jpadie suggests a nonce approach here:



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top