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">

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…

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?');">

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.

Using raw javascript without any div…

You can have this function

function confirmSubmit() {
  if (confirm("Are you sure you want to submit the form?")) {
  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.

Read More:   Error: .post() requires callback functions but got a [object Undefined] not working

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

<form action="<form handler>" method="post" onsubmit="return confirm('Are you sure you want to submit?')">

is this javascript can be store diferent from tag “form”, replace

return confirm(...)

with something like

return sendata(...)

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts