Jump to content

[SOLVED] delete item from cart....window.location.href ..better way?


mo

Recommended Posts

On my cart page I have a delete button next to each item in the cart. The delete button is defined as follows and when clicked, should set the GET variables which should trigger my if statement.

 

However, the page reloads, no GET variables are set and nothing happens.

 

<td class=\"dataListItem\">
<input type=\"image\" class=\"image\" src=\"$img_home/icons/delete.gif\" alt=\"Delete\" name=\"btnDelete\" id=\"btnDelete\" value=\"Delete\" onClick=\"window.location.href='".$_SERVER['PHP_SELF']."?action=remove_item&smid=$strID&itemOption=$strOptions'\">
</td></tr>

 

I tried putting the item variables I need passed on click in the value="" tag but this is always equal to the values for the last item printed in my list.

 

I do not want to add check boxes next to items and have a button at the bottom of the cart page to delete, than loop at the checked boxes and delete items. I would like to have the page reload with the GET variables set. This was working at one point.

 

Any ideas.

Link to comment
Share on other sites

I don't see any opening php tags unless that is done outside of what you've shown us.

 

Show more of the code.

 

Sorry, more code below.

 

<?php
require_once './config/global.php';
require_once MY_DIR.'/access.php';
require_once MY_DIR.'/functions.php';

header("Cache-Control: no-cache");
header("Pragma: nocache");

if($_GET["action"] == "remove_item") {
RemoveItem($_GET["smid"],$_GET['itemOption']);
}

echo "<html>
        <head></head>
        <body>";

echo "<form id=\"CartForm\" name=\"CartForm\" method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
        <div id=\"DataListForm\">";

echo "<table class=\"dataList\" align=\"center\">";

$q1 = mysql_query("SELECT * FROM `my_cart_table`
                   WHERE `cart_id` = '$FakeID'") or die(mysql_error());
			   
while($ArrayRow = mysql_fetch_array($q1)) {

	echo "<tr><td class=\"dataListItem\">$strID</td>
			  <td class=\"dataListItem\">
			  <a href=\"dispmenu.php?storeid=$strStoreId&storename=".urlencode($strStoreName)."\">$strStoreName</a></td>
			  <td class=\"dataListItem\">$strTitle</td>
			  <td class=\"dataListItem\">$strRecipient</td>
			  <td class=\"dataListItem\">$strComments</td>
			  <td class=\"dataListItem\">$strProdSize</td>
			  <td class=\"dataListItem\">
			  <input id=\"qID[]\" name=\"qID[]\" type=\"text\" size=\"5\" value=\"$strQty\" maxlength=\"5\">
			  <input type=\"hidden\" class=\"hidden\" id=\"pid[]\" name=\"pid[]\" value=\"$strID\">
			  <input type=\"hidden\" class=\"hidden\" id=\"optionStr[]\" name=\"optionStr[]\" value=\"$strOptions\">
			  </td>
			  <td class=\"dataListItem\">$strPrice</td>
			  <td class=\"dataListItem\" colspan=\"2\">$strTotal</td>
			  <td class=\"dataListItem\">
			  <input type=\"image\" class=\"image\" src=\"$img_home/icons/delete.gif\" alt=\"Delete\" name=\"btnDelete\" id=\"btnDelete\" 
			  value=\"Delete\" onClick=\"window.location.href='".$_SERVER['PHP_SELF']."?action=remove_item&smid=$strID&itemOption=$strOptions'\">
			  </td></tr>";

}

echo "</table>
         </div>
        </form>
        </body>
        </html>";



?>

Link to comment
Share on other sites

Do you have a live version and/or have you viewed the source to ensure that the link is actually being built properly?

 

yes. The rendered version is below.

 

<html>
<td class="dataListItem">
			  <input type="image" class="image" src="https://mysite.com/img/icons/delete.gif" alt="Delete" name="btnDelete" id="btnDelete" 
			  value="Delete" onClick="window.location.href='/cart.php?action=remove_item&smid=43&itemOption=1,5,'">
			  </td>
</html>

Link to comment
Share on other sites

What's weird is if I change my check to add isset() to the delete button. The delete works but only if I click the button than refresh the page again. I have to make the page reload twice. Also, I still cannot echo the GET variables.

 

if($_GET["action"] == "remove_item" || isset($_POST['btnDelete'])) {
	if($_GET["smid"] != ""){
		RemoveItem($_GET["smid"],$_GET['itemOption']);
	}
}

 

 

Link to comment
Share on other sites

After testing, the real issue is that the form needs to be reloaded when deleting the lowest level item in the cart. Meaning if I have 3 items in my cart. I can delete item 1 and 2 with no problem. However when I try to delete item 3, I need to refresh the page again before item 3 is removed.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.