php sessions and forms

Associate
Joined
12 Jun 2003
Posts
898
Location
Wicklow, Ireland
how's it going guys and guyettes ;).

i'm building a very basic shopping cart system from scratch at the moment. (only learning PHP for 2 days and HTML for 2 months), and i have the basics down.

the store contains three books, each with a listing and drop down box for qty and a price and a "buy now", basically when i click buy now it stores the qty of books selected in the $_SESSION array.

i have two pages

1.bookstore.php - this is where the books/information and buy now buttons are located.
2.cart.php - this emulates the shopping cart stating how many items are in the cart and their totals etc.

now what i want to be able to do is to have a button that says "empty shopping cart" and it will do all that and refresh the page to say "shopping cart is empty."

i'm not using any database software for this mainly because it's only a simulation plus i've no access to any database :D

here's my code if you need it.

cart.php
PHP:
<html>
	<head>
		 <title>Added to cart</title>
		 <link rel="stylesheet" href="style.css">
	</head>
	
	<body>
	
	<div class="main">
	
	<?php
	
		//session_destroy() ;  //used to destroy current session data.
	
		function caltotal($qty, $price)
	  {
		  $tot = $qty * $price ; 
		  return $tot ; 
  	}
	
		
		session_start();
		
		$quantity = $_POST['qty'] ;
		$item = $_POST['item'] ;
		
		$phpprice = 20 ; 
		$dummiesprice = 10 ; 
		$sqlprice = 25 ; 
		
		
		
		if($item == "Programming PHP")
		{
				if(isset ($_SESSION['phpqty']) )
				{
					$_SESSION['phpqty'] += $quantity ;
				}
				else
				{
					$_SESSION['phpqty'] = 1 ;
				}
		}
		
		elseif($item == "Building a Website for Dumies")
		{
				
				if(isset ($_SESSION['dummiesqty']) )
				{
					$_SESSION['dummiesqty'] += $quantity ;
				}
				else
				{
					$_SESSION['dummiesqty'] = 1 ;
				}
		}
		else
		{
				if(isset ($_SESSION['sqlqty']) )
				{
					$_SESSION['sqlqty'] += $quantity ;
				}
				else
				{
					$_SESSION['sqlqty'] = 1 ;
				}
		}
		
		$phptotal = caltotal($_SESSION['phpqty'], $phpprice) ;
		$dummiestotal = caltotal($_SESSION['dummiesqty'], $dummiesprice) ;
		$sqltotal = caltotal($_SESSION['sqlqty'], $sqlprice) ;
		
		
		echo "	<table align=\"center\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
														<tr>
															<td align=\"center\" colspan=\"6\">Shopping Cart</td>
														</tr>
												
														<tr bgcolor=\"#99CC00\"> 
															<td align=\"center\">book</td>
															<td align=\"center\">info</td>
															<td align=\"center\">price</td>
															<td align=\"center\">qty</td>
															<td align=\"center\">total</td>
															<td align=\"center\">remove</td>
												</tr>
				 ";
		
		
		
		
		
		if($_SESSION['phpqty'] == 0 && $_SESSION['dummiesqty'] == 0 && $_SESSION['sqlqty'] == 0)
		{
			echo " <tr>
							<td align=\"center\" colspan=\"4\"> Shopping cart is empty</td>
						 </tr>
					 " ; 
		}
		
		if($_SESSION['phpqty'] != 0)
		{
			echo "	<tr>
								<td><img src=\"phporeilly.jpg\"/></td>
								
								<td>Title: Building a Web Site for Dummies</br>
										Author: David A. Crowder</br>
										Publishers: Hungry Minds
								</td>
								
								<td align=\"center\">€20</td>
								
								<td align=\"center\">" . $_SESSION['phpqty'] . "</td> 
								<td align=\"center\">" . $phptotal . "</td>								
								
					 		</tr>";
		}
			
		if($_SESSION['dummiesqty'] != 0)
		{
			echo "	<tr>
								<td><img src=\"websitedummies.jpg\"/></td>
									
								<td>Title: Building a Web Site for Dummies</br>
										Author: David A. Crowder</br>
										Publishers: Hungry Minds
								</td>
										
								<td align=\"center\">€10</td>
								
								<td align=\"center\">" . $_SESSION['dummiesqty'] . "</td> 
								<td align=\"center\">" . $dummiestotal . "</td>								
								
					 		</tr>";
		}
		
		if($_SESSION['sqlqty'] != 0)
		{
			echo "	<tr>
								<td><img src=\"mysql.jpg\"/></td>
										
								<td>Title: Learning MySQL	</br>
										Author: Hugh E. Williams	</br>
										Publishers: O'Reilly
								</td>
										
								<td align=\"center\">€25</td>
								
								<td align=\"center\">" . $_SESSION['sqlqty'] . "</td> 
								<td align=\"center\">" . $sqltotal . "</td>								
								
					 		</tr>";
		}
		
		echo "<tr>
						<td>shopping cart functions:
								<form action=\"emptycart.php\" method=\"post\">
									<input type=\"submit\" value=\"clear shopping cart\"/>
								</form>
						
				 ";
		
		echo "	<tr>
							<td align=\"center\" colspan=\"6\">
								the current time is: " . gmstrftime("%X </br> time zone: %Z",time()) . "</br>
								today's date is: " . date("d/m/Y") .
								"</td>
						</tr>
				
				</table>
				";
		
	?>
	
	
	</div>
	
	</body>
	
</html>


Code:
shopping cart functions:
          <form action=\"emptycart.php\" method=\"post\">						<input type=\"submit\" value=\"clear shopping cart\"/>
          </form>

this here basically just goes to emptycary.php that has the line
PHP:
session_destroy()
; in it.

what i want to happen is when the user clicks "clear shopping cart" the session array is deleted.

can this be done?

if i haven't explain this well enough please slap me and i'll go into more detail if i can.

also since i'm only learning PHP for 2 days, is it acceptable to write code like this or is it frowned upon

PHP:
echo " <table width=\"100%\"> " ;
Thanks.
 
Code:
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}

// Finally, destroy the session.
session_destroy();
?>
from http://php.net/session_destroy
 
use a switch there instead of the long if, elseif, else. Preferably don't use that at all, its clumsy and lacks scalability.

Code repetition in there is rife, find a better way.

And what do you mean no access to a database? Whats the point in it without a database. XAMPP will set you up with mysql dead easily.
 
i'm a php noob too but i use single quotes when using the echo statement. it means i don't have to escape the double quotes in my html... :)

Code:
echo '<p><img src="'.$img.'" alt="image" /></p>';

whether it's good/bad practice i don't know but it's easier. :D
 
Back
Top Bottom