[SOLVED] 'Remove' section not removing until clicked a secon

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

[SOLVED] 'Remove' section not removing until clicked a secon

Post by dardsemail »

I have listed code below. The problem I'm having is with the 'remove' section. When I try to remove an order, the order still appears on the page until I click on 'remove' a second time.

Any suggestions?

Code: Select all

<?php
ob_start();
session_start();
$authorizedUsers = "visitor,admin";
$donotCheckaccess = "false";
$_SESSION['prevUrl']="orders.php";

	
if (isset($_SESSION['frm_username']))
	{	
		showPage();
	}
	else 
	{
		header("Location:login.php");
	}

switch(@$_GET["action"]) 

	{
		case "remove_orders":
		{
			RemoveOrder($_GET["orderID"]);
		}
	}

	function RemoveOrder($orderID)
	{
		// Uses an SQL delete statement to remove an item from
		// the users cart

		include ('connection/db.php');
				
		mysql_query("DELETE FROM orders WHERE orderID = $orderID");
		showPage();
	}

function showPage()
{

error_reporting (E_ALL);
		
function showOrders()
{

	include ('connection/db.php');
	$orderQuery="SELECT * FROM orders WHERE userID='" . $_SESSION['userID'] . "'";
	//Retrieve the item details of the cart 
	$result = mysql_query ($orderQuery) or die("ERROR, ERROR!".mysql_error()); 
	$numRows = mysql_num_rows ($result);
	
	if ($numRows == 0)
	{
				echo "<h4><align =",center,">You do not have any open orders at present.</h4><br />";
		}
		else
		{
				echo "
            <table align=",center,">
			<tr>
				<td><h1>Your Shopping Cart</h1>
				</td>
				</tr>
              <tr>
                <td><h4>Order ID</h4></td>
                <td><h4>Date Submitted</h4></td>
                <td><h4>Date Shipped</h4></td>
                <td><h4>Total Value of Order</h4></td>
				<td><h4>Remove?</h4></td>
              </tr>";		
	//Go through each of the items in the cart
	while ($row=@mysql_fetch_array($result))
	{

		//Show the details of the item
		echo "\n\t<td><h3>";
		echo $row["orderID"];
		echo "</h3></td>";
		
		//Show the details of the item
		echo "\n\t<td><h3>";
		echo $row["dateSubmitted"];
		echo "</h3></td>";
		
		//Show the details of the item
		echo "\n\t<td><h3>";
		echo $row["dateShipped"];
		echo "</h3></td>";
		
		//Show the details of the item
		echo "\n\t<td><h3>";
		echo $row["total"];
		echo "</h3></td>";
		
		//Link to remove item - blank for now
		echo "\n\t<td>";		
		echo "<a href=orders.php?action=remove_orders&orderID=",$row["orderID"],">Remove</a>";
		echo "</td>";
		echo "\n</tr>";				
	}
	}
}


?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if (isset($_SESSION['frm_username']))
   {   
      showPage();
   }
   else
   {
      header("Location:login.php");
   }

maybe it's hitting this before it can remove the data? What happens if you force refresh the page after you hit the remove link?
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

If I refresh it, the data is gone - so its clearly working. I just can't figure out how to run this so it works... I can't seem to get the ordering right.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

run the quoted code as the default case of the switch on action...
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I changed the switch section to be the following and its still not working:

Code: Select all

<?php
switch(@$_GET["action"]) 

	{
		case "remove_orders":
		{
			RemoveOrder($_GET["orderID"]);
		}
		default:
		{
			showPage();
			}
	}

?>
Is that what you had meant? I wasn't sure where to place it.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

just putting another solution across....

Could it be a browser caching problem??
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I don't think its a caching problem. I actually copied the code from another application I have that works properly. Here is the code from which it was copied:

Code: Select all

<?php
switch(@$_GET["action"]) 

	{
		case "remove_item":
		{
			RemoveItem($_GET["piecenum"]);
			break;
		}
	}

	function RemoveItem($piecenum)
	{
		// Uses an SQL delete statement to remove an item from
		// the users cart

		include ('connection/db.php');
				
		mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND piecenum = $piecenum");
	}

?>
This code doesn't require that I click twice. Very strange...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

if (isset($_SESSION['frm_username']))
{
showPage();
}
else
{
header("Location:login.php");
}
Try moving that code below your switch() statement. The problem looks like you are first displaying the page then doing the delete, rather than doing the delete then displaying the page.
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

That's the ticket! Thanks so much!!!!
Post Reply