简体   繁体   中英

Delete button for first row not working in PHP cart I'm trying to delete items on button click

//for each
foreach($_SESSION as $products) {

//calc total of each item
$bill = intval($products[1] * $products[2]);

echo "<tr>";
echo '<form action ="editcart.php" method="post">';

//item name
echo "<input type='hidden' name='name'value='".$products[0]."'>";
echo "<td class='product_name'> ".$products[0]."</td>";

// item price
echo "<input type='hidden' name='price'value='".$products[1]."'>";
echo "<td class='product-price' > $".$products[1]."</td>";

//quantity
echo "<input type='hidden' name='q'value='".$products[2]."'>";
echo '<td class="product_quantity">'.$products[2].'</td>';

//total of each item
echo "<input type='hidden' name='d'value='".$bill."'>";
echo '<td class="product_total">$'.$bill.'</td>';

//delete button
echo '<td><input type="submit" name="event1" value="Delete"></td>';

echo "</form>";
echo "</tr>";

$total += intval($bill);
}

When I try to delete the item on the first row the page is not directing to editcart.php while for the rest of the items in the table it works properly

<?PHP
session_start();



$name = $_POST['name'];
$price = $_POST['price'];
$quantity = $_POST['q'];
$d = $_POST['d'];
$event = $_POST['event1'];


$product = array($name, $price, $quantity, $d);



if ($event == "Delete") {
unset($_SESSION[$name]);
}

header('location:viewCart.php');

This is editcart.php code above on delete button click on the first-row page is not directing towards this file.

Below is the address on the address bar of the browser for the first-row delete button.

http://localhost:8080/Project/viewCart.php?name=Cute+Cat+Earrings&price=5&q=3&d=15&event1=Delete#

<form action ="#" method="post">

    foreach($_SESSION as $products) {

    //calc total of each item
    $bill = intval($products[1] * $products[2]);

    echo "<tr>";

    //item name
    echo "<input type='hidden' name='name'value='".$products[0]."'>";
    echo "<td class='product_name'> ".$products[0]."</td>";

    // item price
    echo "<input type='hidden' name='price'value='".$products[1]."'>";
    echo "<td class='product-price' > $".$products[1]."</td>";

    //quantity
    echo "<input type='hidden' name='q'value='".$products[2]."'>";
    echo '<td class="product_quantity">'.$products[2].'</td>';

    //total of each item
    echo "<input type='hidden' name='d'value='".$bill."'>";
    echo '<td class="product_total">$'.$bill.'</td>';

    //delete button
    echo '<td><a href="editcart.php?name=<?php echo $products[0]; ?>&event=Delete">Delete</a></td>';


    echo "</tr>";

    $total += intval($bill);
    }

    </form>

    **PHP FILE CODE**


         <?PHP
session_start();
$name = $_GET['name'];
$event = $_GET['event'];

if ($event == "Delete")
{
    unset($_SESSION[$name]);
}

header('location:viewCart.php');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM