Member Avatar for TheFearful

Hey everyone, I have a question. I have a web app where you have a table that will either update, delete, or add a user. The update works great but the delete is weird. It deletes just fine, but I ask for a confirmation first before the action is completed.
I am using PHP, jquery/javascript, html, and MySQL
This is the code that I used for the button

 echo "<td><button type='submit' name='delete' value='".$row['UserID']."' class='delete-button'>Delete</button></td>";

This is the jquery/javascript that I am using

 $('.delete-button').on('click', function()
        {
                var conf=confirm("Are you sure?");
                if(conf==false)
                {
                        window.close();
                }
                       });
});

When the user hits the button, the popup comes out asking if they are sure. If they hit the cancel button, the window will close. If they hit okay, they will go to the next page where the Query will be executed to delete the entry.
However, sometimes but not all the time, the entry will be deleted even if the user hits the cancel button. Would anyone know why that is happening?

I would guess it is because you're not actually stopping the button submission by return false; or event.stopPropagation() so your form is still submitting sometimes. All the cancel button does is close the window but you've still clicked the submit button before that.
Why it only happens sometimes, I don't know, probably a matter of timing before the window closes.

Member Avatar for TheFearful

I did a preventDefault method and it worked perfectly! Thank you for the input!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.