Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirm Before Form Submission

Tags:

html

forms

php

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?');"
like image 664
Sahar Alsadah Avatar asked Sep 20 '16 11:09

Sahar Alsadah


People also ask

How do I know how submit a 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.

What is form submit?

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()

Which one of the following events can be used to display a confirmation box when a user submits a form in JavaScript?

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.

How do you show success message after submitting HTML form?

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.


1 Answers

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(&quot;Are you sure you want to submit this form?&quot;);\""

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.

like image 100
Quentin Avatar answered Sep 22 '22 02:09

Quentin