Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML - Form Submit Button Confirmation Dialog

this is a general form code

<form name="search_form" action="" method="POST">
<input type="text" name="search_text">          
<input type="submit" name="search_bt" value="Go">
</form>

is there a way could have a confirmation dialog saying 'Yes'\'No' or 'Confirm'\'Cancel' etc...

One way i figured of dong is is with CSS Layer and JavaScript and Php... that have a php isset(){} chechk on the button and when set display a Div displayed with two buttons and onclick=func() JS function of those buttons have a php variable(flag) set and then i can if(flag){} to continue or skip some code...

well that is going to work and plus point is that i can have a well themed dialog box but i just wanna make my life easier...

like image 385
Moon Avatar asked Sep 03 '10 01:09

Moon


People also ask

How do I make a confirm button in HTML?

You can create a JavaScript confirmation box that offers yes and no options by using the confirm() method. The confirm() method will display a dialog box with a custom message that you can specify as its argument.

How do I add a confirmation pop up in HTML?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

How do I confirm a form submit?

To confirm before a form submit with JavaScript, we can call the confirm function. to add a form. to select a form with querySelector . And then we set form.


3 Answers

You can also do it with one line in the form tag itself

<form action="exampleHandlerPage.php" method="post" onsubmit="return confirm('Are you sure you want to submit?');"> 
like image 69
acqu13sce Avatar answered Oct 01 '22 11:10

acqu13sce


If you have 2 or more submit buttons in one form:

<input type="submit" value="Edit"> <input type="submit" name="delete" value="Delete" onclick="return confirm('Confirm, please.');"> 

The dialog shows up only when you click the Delete button.

like image 34
matepal297 Avatar answered Oct 01 '22 11:10

matepal297


Using raw javascript without any div...

You can have this function

function confirmSubmit() {
  if (confirm("Are you sure you want to submit the form?")) {
    document.getElementById("FORM_ID").submit();
  }
  return false;
}

And you can call that function from the onsubmit event in the form, or on the onclick event in the button.

By the way, have you heard about JQuery. Its a JS Library with a lot of helpful things that give you a comfort and beauty way of coding javascript.

As an example of what you want to get done, take this confirmation dialog from JQuery as example

like image 44
Garis M Suero Avatar answered Oct 01 '22 12:10

Garis M Suero