Why is every button opening php file

Viewed 39

I'm running my website using XAMPP. Is there any way to prevent type="submit" button from opening php file, but still do given tasks from php. I don't know why is it opening php file on website. Here's my code:

<form action="receive.php" method="post">
    <button type="submit" id="delete-product-btn" class="rightBTN" name="deletee">MASS DELETE</button>
</form>

Here's php file and what it's displaying:

<?php
    $conn = new mysqli('localhost', 'root', '', 'test');
    $sql = "SELECT * from ScandiwebTest";
    $stats = mysqli_query($conn, $sql) or die(mysql_error());
    if($conn->connect_error){
        die("Connection Failed: " .$conn->connection_error);
    }else{
        
    }
    if ($result = mysqli_query($conn, $sql)) {
        $rowcount = mysqli_num_rows( $result );
        $quantity = $rowcount;
    }
    while($row = mysqli_fetch_array($stats)){
        $id = $row[0];
        $sku = $row[1];
        $name = $row[2];
        $price = $row[3];
        $productType = $row[4];
        $size = $row[5];
        $weight = $row[6];
        $height = $row[7];
        $width = $row[8];
        $length = $row[9];
        /*Those are displayed on opened php file*/
        echo "<div class='product'>";
            echo "<input type='checkbox' id='checkbox$id' name='delete-checkbox[]' value='$id'></input>";
            echo "<div class='info'>";
                echo "<p> {$sku} </p>";
                echo "<p> {$name} </p>";
                echo "<p> {$price}$ </p>";
                if($productType == "d"){
                    echo "<p> {$size}MB </p>";
                }
                if($productType == "b"){
                    echo "<p> {$weight}KG </p>";
                }
                if($productType == "f"){
                    echo "<p> {$height}x{$width}x{$length} </p>";
                }
            echo "</div>";
        echo "</div>";
        /*Those are displayed on opened php file*/
    }
    if(isset($_POST['delete']) && (isset($_POST["delete-checkbox"]) != 0 || isset($_POST["delete-checkbox"]) != null)){
        $ids = $_POST["delete-checkbox"];
        $extract_id = implode(",", $ids);
        $query = "DELETE FROM ScandiwebTest WHERE id IN($extract_id)";
        $query_run = mysqli_query($conn, $query);
        if($query_run){
            $_SESSION['status'] = "Product deleted successfully!";
            header("Location: index.html");
            exit;
        }else{
            echo "<h1>VMUSHAOB</h1>";
            $_SESSION['status'] = "Product wasn't deleted!";
            header("Location: index.html");
            exit;
        }
    }
?>
1 Answers

According to comments, you have a form tag that you have no use for but are still using it.

Below is a javascript that will disable the submit event on EVERYTHING on the page.

document.body.addEventListener("submit",function(e){
 e.preventDefault();
});
<form action="receive.php" method="post">
    <button id="delete-product-btn" class="rightBTN" name="deletee">MASS DELETE</button>
</form>

Related