How to remove get parameter value after form submission

Viewed 184

I have a form and i can add datas to the table and store it in the session. what my problem is when ever i click remove button the id is in the url. so i cant add other datas.

I need to get rid of the get value after i click remove

its like this http://localhost:8085/examples/buyme.php?del=2 i need it to be like this http://localhost:8085/examples/buyme.php

 <table class="table">
    <thead>
  
            <tr>
                <th>ID</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Quantity</th>
                
                <th>Action</th>

            </tr>
        </thead>
    

<tbody>
            
                    <tr>
            
                    </tr>
                <?php
                if(!empty($_SESSION["cart"])){
                    $values;
                    $total=0;
                    foreach($_SESSION["cart"] as $keys=>$values){
                        $amount = $values["quantity"]*$values["price"];
                        $total=$total+$amount;
                ?>
                         <tr>
                             <td> <?php echo $values["id"] ?> </td>
                      <td> <?php echo $values["pname"] ?> </td>
                      <td> <?php echo $values["price"] ?></td>
                      <td><?php echo $values["quantity"] ?> </td>
                    
                    <td><a href="buyme.php?del=<?php echo $values["id"]  ?>"> Remove</a> 
                             <a href="#editProductModal" class="edit" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i></a>
                            
                             
                             
                             </td>
                      <td> </td>
                      
                  </tr>
                 
                <?php   }?>
                <tr>
                      <td></td>
                      <td></td>
                    <td></td>
                      <td>Total </td>
                     <td><?php echo $total ?> </td>
                    
                      <td> </td>
                    <?php } ?>  
                  </tr>
                    <!-- Removing from cart -->
            <?php   
                if(isset($_GET["del"])){
                    foreach($_SESSION["cart"] as $keys=>$values){
                    if($values["id"]==$_GET["del"])
                    {
                        unset($_SESSION["cart"][$keys]);
                       

                        echo "<meta http-equiv='refresh' content='0'>";//to refresh page after submit
                    }
                    }
                    
                }
                    
                
                ?>
                <br>
  <br>
 
                    
                 </tbody>
                  
</table>

Please help me thanks in advance

2 Answers

You need to redirect page to buyme.php after deleting/unsetting session values, for that replace this line in your code:

echo "<meta http-equiv='refresh' content='0'>";//to refresh page after submit

with this:

header('Location: http://localhost:8085/examples/buyme.php');

I would be more safe to send the id for deletion not by a GET request but by a POST request. To make this possible you have to insert a <form> with method="POST" and a hidden input field with name del and the id you want to delete for each item. Then you have to handle the POST call in your script.

For example like this:

if($_POST['del']) {
  # your delete action here
}

By using a POST request it is not possible to change the id via url.

Related