I created a PHP page to delete from my database. This PHP page is the action of a form submission. The form is submitted by clicking a pic of "X" sign. How can I ask user to confirm before the submission of the form and therfore deleting from my database?
This is my code:
<form method='POST' action='delete.php'><input type='image' src='images/delete.png' class='del' alt='Submit Form' />
I tried to do it inline by adding this to my form tag, but it did not work:
onsubmit="return confirm('Are you sure you want to submit this form?');"
Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method. Remember in place of submit define the name of submit button. After clicking on submit button this action will work as POST method.
The form submit() Method in HTML DOM is used to send the form data to the web-server. It works as same as submit button. It does not contain any parameters. Syntax: formObject.submit()
Given <a> element and the task is to display the confirmation message when clicking the <a> link, with the help of JavaScript and jQuery. onclick Event: This event occurs when the user clicks on an element.
There are 2 general ways to show a message after submitting an HTML form: Use Javascript AJAX to submit the form and show a message when the processing is complete. Submit the form as usual, and have the server-side script pass back a flag to show the message.
From a comment by the OP:
echo "onclick='return confirm(\'Are you sure you want to submit this form?\');'"
You can't use single quotes in an attribute value delimited with single quotes.
Your options are:
Use double quotes in the JS
echo "onclick='return confirm(\"Are you sure you want to submit this form?\");'"
Use double quotes in the HTML
echo "onclick=\"return confirm('Are you sure you want to submit this form?');\""
Use entities
echo "onclick='return confirm("Are you sure you want to submit this form?");\""
When out you put JavaScript inside an HTML attribute value inside a PHP string you have three different languages all mixed together, and you have to be very, very careful with your escaping so that you escape the right characters for the right languages at the right times.
As a rule of thumb, it is better to keep that contents of PHP strings to a minimum. Only drop into PHP mode when you have a variable.
?>
<form method='POST' action='delete.php'>
<input type='image' src='images/delete.png' class='del' alt='Submit Form' onclick="return confirm('Are you sure you want to submit this form?');" />
</form>
<?php
For the same reason, it is better to keep your JavaScript in an external file and attach event handlers with addEventListener
instead of onFOO
attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With