Home > database >  Remove cart button and function
Remove cart button and function

Time:07-06

I already create a delete button, but when I click it no showing any error message and the cart item and the item in database no get deleted.

<td >
    <button type="button"  value="<?= $citem['prod_id']; ?>">Delete</button>
</td>

This is the delete button function

if(isset($_POST['delete_cart_btn']))
{
    $prod_id = mysqli_real_escape_string($con, $_POST['prod_id']);

    $prod_query = "SELECT * FROM carts WHERE id='$prod_id' ";
    $prod_query_run = mysqli_query($con, $prod_query);
    $prod_data = mysqli_fetch_array($prod_query_run);
    $image = $prod_data['image'];

    $delete_query = "DELETE FROM carts WHERE id='$prod_id' ";
    $delete_query_run = mysqli_query($con, $delete_query);

    if($delete_query_run)
    {
         if(file_exists("../assets/images/products/".$image))
         {
             unlink("../assets/images/products/".$image);
         }

         echo 200;
    }
    else
    {
         echo 404;
    }
 }

Here my database enter image description here

Here the button cart script and the button function

enter image description here enter image description here

Here the full code the cart

<div id="fullCart" >
    <div >
        <table >
            <thead colspan="4">
            <tr>
                <p ><strong><span style="font-size: 25px;"><i  style="font-size:25px;color:black"></i> My Cart</span></strong></p>
            </tr>
            </thead>
            <hr>
            <thead>
            <tr>
                <th >Product</th>
                <th >Quantity</th>
                <th >Price</th>
                <th >Total</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
                <?php 
                    $subTotal   = 0;
                    $quantity   = 0;
                    $tax        = 10;
                    $items = getCartItems();
                    foreach ($items as $citem) {
                      $subTotal  = $citem['prod_qty'] * $citem['selling_price'];
                      $quantity  = $citem['prod_qty'];
                ?>
                 
                <tr id="item_<?= $citem['prod_id']; ?>">
                <td >
                    <div >
                        <img  src="assets/images/products/<?= $citem['image']; ?>" style="width: 100px; height: 100px;">
                        <h4  style="position:relative; left:110px; top:-100px;"><?= $citem['name']; ?></h4>
                    </div>
                </td>
                <td >
                    <strong><?= $citem['prod_qty']; ?></strong>
                </td>
                <td >
                    <strong><span style="font-size: 18px;">$</span><span id="price"><?= number_format( $citem['selling_price'], 2 ); ?></span>
                    </strong>
                </td>
                <td >
                    <strong><span style="font-size: 18px;">$</span><span id="totalPrice_<?= $citem['prod_id']; ?>"><?= number_format( $citem['prod_qty'] * $citem['selling_price'], 2 ); ?></span>
                    </strong>
                </td>
                <td >
                    <button type="button"  name="delete_cart_btn" value="<?= $citem['prod_id']; ?>">Delete</button>
                </td>
            </tr>
            <?php } ?>
            <tr>
                <td colspan="4" align="right">Subtotal</td>
                <td >
                    <strong><span style="font-size: 18px;">$</span>
                        <span id="subTotal"><?= number_format( $subTotal, 2 ); ?></span>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="right">Taxes</td>
                <td >
                    <strong><span style="font-size: 18px;">$</span>
                        <span id="taxes"><?= number_format( $tax * $quantity, 2 ); ?></span>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="right">Total</td>
                <td >
                    <strong><span style="font-size: 18px;">$</span>
                        <span id="finalPrice"><?= number_format( $subTotal ($tax * $quantity), 2 ); ?></span>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="right">
                    <a href="index.php" >
                        <span ></span> Place Order
                    </a>
                </td>
                <td >
                    <a href="checkout.php" >
                        Order <span ></span>
                    </a>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

CodePudding user response:

It seems you don't have a form to submit There is no action when there is no form

Add a form tag and change your button type to submit you need to data pass by POST so i 've added a method="POST" attribute to my form

<form>
       <td  action="file_name.php" method="POST">
                    <button type="submit"  name="delete_cart_btn" value="<?= $citem['prod_id']; ?>">Delete</button>
       </td>
</form>

instead of file_name.php put your php script file name

CodePudding user response:

You did not name the button, so it doesn't send the data in the form.

<td >
    <button type="button"  name="delete_cart_btn" value="<?= $citem['prod_id']; ?>">Delete</button>
</td>

if(isset($_POST['delete_cart_btn']))
{
    $prod_id = mysqli_real_escape_string($con, $_POST['delete_cart_btn']);

    $prod_query = "SELECT * FROM carts WHERE id='$prod_id' ";
    $prod_query_run = mysqli_query($con, $prod_query);
    $prod_data = mysqli_fetch_array($prod_query_run);
    $image = $prod_data['image'];

    $delete_query = "DELETE FROM carts WHERE id='$prod_id' ";
    $delete_query_run = mysqli_query($con, $delete_query);

    if($delete_query_run)
    {
         if(file_exists("../assets/images/products/".$image))
         {
             unlink("../assets/images/products/".$image);
         }

         echo 200;
    }
    else
    {
         echo 404;
    }
 }
  • Related