Adding alert box to my delete button

Hello, i recently added a delete button that deletes the row they want to delete and all works excellent.

Now my question is if it is possible to first pop up a confirm window before the action file in the form gets launched to delete it from the db.

my form:

while($row = $result->fetch_assoc()) { ?>
	<td><?php echo $row['subcategory']; ?></td>
	<td><?php echo $row['costname']; ?></td>
        <!-- Added "€" sign infront of price !-->
	<td><?php echo "€ " . $row['price']; ?></td>
	<td><?php echo $row['info']; ?></td>
        <!-- Added Date Function to convert format !-->
	<td><?php echo date( "d/m/y",strtotime ($row['costdate'])); ?></td>
                <form action = "includes/deletecost.php" method="post">
                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                        <input type="submit" name="delete" value="Verwijder">

and my delete php script:


	//connection to db
	$conn = new mysqli("server information private");
	//check the connection
	if (!$conn) {
	  die("Connection failed:".msqli_connect_error());

	//set id to delete
	$id = $_POST['id'];
	//delete the row with the correct id
	if($stmt = $conn->prepare("DELETE FROM costs WHERE id=?")) {
		$stmt->bind_param("i", $id);
} else {
	echo "Error";

header("Location: ../costs.php");



You have posted in the JavaScript category but your code looks like PHP.

Please render your code as HTML and JavaScript like we would see it in a browser using “show source”, then post it.

If this is a PHP issue, then we would be happy to move your post into the PHP category.

Hello, I’m really sorry i thought the answer would be something in javascript that’s why i put it into that category.

And i’m new to coding so rendering it in html and js is kinda strange to me…
Do you mean that i should put marks around the doc or?

I found the answer so this topic isn’t required anymore but i would like to know for future threads tho.

We look at HTML, CSS, and JavaScript within a web page as it is rendered in a browser. In other words, in its native form. We do not expect someone to post PHP code that needs to be interpreted as HTML, CSS or JS so we can work on a problem in any of those 3 formats. If you are posting PHP code, it should be because you have a question about the PHP code.

This is a continuation of an issue involving using PHP to process the deletion of the record, but the OP would now like to issue a warning to the user when they select the “Delete” button to ask them if they are sure that they would actually like to delete the record. This is where the JavaScript comes in.

This is what I would do, although I’m not sure it’s the best way to do it. I am far from being a JavaScript expert.

<input type='submit' id='delete_record' name='delete' value='Delete' onclick='return checkDelete()'>

Than I would add this script:

<script type="text/javascript">
        function checkDelete(){
            return confirm('Are you sure you want to delete this record?');
1 Like

Thanks for the well explained answer WebMachine!
I added a onclick to the delete button and it works perfectly!

Thanks and my apologies again.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.