Modal Confirmation Dialog on Form Submit: Javascript, jQuery UI, and Thickbox Varieties

jQuery files updated June 23, 2010. Because Thickbox is no longer supported, I may have to drop its example to update the jQuery files in the future.

If you liked this post, you might also like the jQuery.ajax and jQuery.post Form Submit Examples with PHP post.

I wanted to make a nice modal dialog box that would confirm submission of a form. And, specifically, it had to ask if their e-mail address was correct that they listed on the form. Typos, particularly transposed letters, cause a number of undeliverable e-mails. Making matters worse, sometimes by the time they get to the ‘Submit’ button, the all important e-mail field is no longer in view. Yes, despite the web developer’s best efforts, people still manage to type in their own e-mail incorrectly. I’ve done it. We’re all human.

I already knew about the javascript 1.0 method of ‘return confirm’ but I wanted more control over the look and feel. As an added requirement, I wanted to use Thickbox since I already had that loaded into the app. I don’t like throwing in a bunch of js files for every little nuance. If I can use what I already have, it makes the app much leaner.

So, I Googled it. I only found partial solutions. I wanted to go all the way and have the modal box pop up whether the user hit ‘Submit’ or whether they hit enter on the keyboard in as many browsers as possible (hint: IE). Maybe I didn’t look hard enough, but I ended up creating the solution on my own.

Here are three examples (including a complete demo) of a form submission confirmation dialog. One each for just javascript, the jQuery UI dialog box, and Thickbox.

Javascript 1.0

Echo back a message for fun:

<?php if (isset($_POST['emailJS'])){
 echo "<p class='message'>Javascript 1.0 worked!!! Your e-mail address is " .$_POST['emailJS'];
 echo "</p>";
 }?>

For pure javascript it’s painfully straightforward:

<form id="testconfirmJS" name="testconfirmJS" method="post" onsubmit="return confirm('You entered your e-mail address as:\n\n' + document.getElementById('emailJS').value + '\n\nSelect OK if correct or Cancel to edit.')">>
<fieldset>
<legend>Javacript Return Confirm</legend>

<p><label for="email">E-mail:</label><br />

<input id="emailJS" type="text" name="emailJS" value="" /></p>

<p><input type="submit" value="Submit" /></p>

</fieldset>
</form>

And you get something that looks like this:
javascript confirm box

Low overhead, not too much strain on the brain but pretty plain vanilla. And boring. Let’s do something a little fancier.

jQuery UI

The jQuery UI library is awesome. Loads of flexibility and customization available and they hand you everything you need. For this solution we have to load in an additional stylesheet (you can download a custom ready-made css), the jQuery base js, and the jQuery UI js. The jQuery UI site allows you to customize its js file to include only the parts you will use.

And, because this example uses Thickbox which is no longer supported, the jQuery core and jQuery core have not been updated to the latest versions. If you are not using Thickbox, use the latest version of jQuery.

<link rel="stylesheet" type="text/css" href="css/blitzer/jquery-ui-1.8.2.custom.css">

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.2.custom.min.js"></script>

The form is untouched for this approach, but we do have to add some js to the document itself and an additional div element that will hold the contents of our modal box.

Here the we set some parameters for the dialog box including what the buttons should say and do. Then a little jQuery is added so that the e-mail is displayed in the box and the box opens on submit allowing the user to hit the ‘Submit’ button or the enter key.

        $(function(){
                // jQuery UI Dialog    

                $('#dialog').dialog({
                    autoOpen: false,
                    width: 400,
                    modal: true,
                    resizable: false,
                    buttons: {
                        "Submit Form": function() {
                            document.testconfirmJQ.submit();
                        },
                        "Cancel": function() {
                            $(this).dialog("close");
                        }
                    }
                });

                $('form#testconfirmJQ').submit(function(e){
                    e.preventDefault();

                    $("p#dialog-email").html($("input#emailJQ").val());
                    $('#dialog').dialog('open');
                });
        });

Echo back a message for fun:

<?php if (isset($_POST['emailJQ'])){
 echo "<p class='message'>jQuery UI worked!!! Your e-mail address is " .$_POST['emailJQ'];
 echo "</p>";
 }?>

The form is pretty clean and the div containing the text for the dialog box can be placed anywhere on the page.


<form id="testconfirmJQ" name="testconfirmJQ" method="post">
<fieldset>
<legend>jQuery UI Modal Submit</legend>

<p><label for="email">E-mail:</label><br />

<input id="emailJQ" type="text" name="emailJQ" value="" /></p>

<p><input type="submit" value="Submit" /></p>

</fieldset>
</form>

<div id="dialog" title="Verify Form jQuery UI Style">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 0 0;"></span> You entered your e-mail address as:</p>
<p id="dialog-email"></p>
<p>If this is correct, click Submit Form.</p>
<p>To edit, click Cancel.<p>
</div>

Using this method you get a much prettier result:
jQuery dialog box

Thickbox method

The ever helpful and massively adaptable Thickbox was easily coded to do this as well. Thickbox also requires its own stylesheet and js along with the base jQuery file. Many apps have this already loaded since Thickbox is so wildly popular.

<link rel="stylesheet" type="text/css" href="css/thickbox.css">

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/thickbox-compressed.js"></script>

As with the jQuery UI method, we add some js to the document to put in the e-mail from the form, open the Thickbox, and instruct the buttons on what actions to take when clicked.

        $(function(){
                //Thickbox

                $('form#testconfirmTB').submit(function(e){
                    e.preventDefault();

                    $("p#TB-email").html($("input#emailTB").val());
                    tb_show('Verify Form Thickbox Style','TB_inline?height=155&amp;width=300&amp;inlineId=TBcontent');

                });

                $('input#TBcancel').click(function(){
                    tb_remove();
                });

                $('input#TBsubmit').click(function(){
                    document.testconfirmTB.submit();
                });
        });

Echo back a message for fun:

<?php if (isset($_POST['emailTB'])){
 echo "<p class='message'>Thickbox worked!!! Your e-mail address is " .$_POST['emailTB'];
 echo "</p>";
 }?>

The form is also clean for this method and a div is added for the Thickbox content.

<form id="testconfirmTB" name="testconfirmTB" method="post">
<fieldset>
<legend>Thickbox Modal Submit</legend>

<p><label for="email">E-mail:</label><br />

<input id="emailTB" type="text" name="emailTB" value="" /></p>

<p><input type="submit" value="Submit" /></p>

</fieldset>
</form>

<div id="TBcontent" style="display: none;">
<p>You entered your e-mail address as:</p><p id="TB-email"></p>
<p>If this is correct, click Submit Form.</p><p>To edit, click Cancel.<p>
<input type="submit" id="TBcancel" value="Cancel" />
<input type="submit" id="TBsubmit" value="Submit Form" />
</div>

The Thickbox, generically styled (the default styling actually), looks like this:
Thickbox modal confirm box

Three flavors for confirming on form submission. Enjoy.

Recommended:

Demo

 

Further Reading:

  1. jQuery.ajax and jQuery.post Form Submit Examples with PHP
  2. jQuery.ajax and jQuery.post Form Submit Examples with ColdFusion
  3. jQuery Modal Dialog Close on Overlay Click
  4. Output Javascript or Jquery Variables to Firebug Console
  5. Using jQuery Datepicker and Dialog Box To Select Date Range

119 Comments

1 4 5 6
  • August 26, 2011 - 8:59 pm | Permalink

    @keith
    Just read through your comments. I need to take a look at that onsubmit function you mentioned and give that a try. For now, the link I posted is no better than what you have done so far.

  • September 4, 2011 - 5:58 pm | Permalink

    You made my day, I had been looking for this for a while. ¡Thanks a lot!

  • Ricardo
    September 6, 2011 - 10:09 am | Permalink

    Hello, I am wondering if you could help me with something regarding JQuery UI Dialog. I have a table with a certain number of rows, each one referring to a person’s information. The rows are generated with PHP and MYSQL. I add to each row (or user) the ability to delete it. I do this with dynamically generated forms, which are basically sending the user-id to another page for deletion.
    I tried implementing the JQuery UI Dialogs – following your example – and every time I click on a “Delete” button I get the Dialog for confirmation. The problem is, that – no matter which user I want to delete – the Dialog only reads the information of the first entry from the database. So I will always delete the first user!
    Do you have an idea of how can I implement the Dialog with different forms with same class names which are dynamically generated?

    I call the Dialog here:

    $(function() {
    $("#Dialog-Confirm").dialog({
    autoOpen: false,
    resizable: false,
    draggable: false,
    modal: true,
    buttons: {
    "Aceptar": function() {
    $( this ).dialog( "close" );
    },
    "Cancelar": function() {
    $( this ).dialog( "close" );
    }
    }
    });

    $(".Form-Delete").submit(function(e) {
    e.preventDefault();

    $("p#Dialog-Object").html($("input.Object").val());
    $("#Dialog-Confirm").dialog("open");
    });

    });

    And my form is like this. Of course this repeats for every entry…

    <input name="Borrar" type="hidden" value=""/>
    <input name="Object" class="Object" type="hidden" value=""/>

    And finally…

    Eliminar la Liga:

    Thanks a lot Jen!

  • September 6, 2011 - 11:50 am | Permalink

    @Ricardo

    You need to identify which one you want to delete. It is deleting the first one because that is the first matching entry.

    You can use a combination of the form id and input class to target the right entry. Por ejemplo:

    $("p#Dialog-Object").html($("form#form1 input.Object").val());

    You need to uniquely identify either the form or the input.Object. jquery is good, but it can not read your mind…yet.

  • Ricardo
    September 6, 2011 - 2:12 pm | Permalink

    @Jen

    Thanks for your quick answer. I understand the concept but I have no clue how to implement it. Is it possible to generate an id or a class number with php?

    <input name="Borrar" type="hidden" value="”/>
    <input name="Object" class="Object" type="hidden" value="”/>

    There is my form… As you can see, all will have the same Class name. What I am interested in, is that the value $id_liga gets to the next page, so the entry can be deleted. Reading $descripcion is just for reference purposes. Still, I am just a beginner with JQuery and I cannot think how I can identify either the form or the inputs and apply them here:

    $(“.Form-Delete”).submit(function(e) {
    e.preventDefault();

    $(“p#Dialog-Object”).html($(“form#form1 input.Object”).val());
    $(“#Dialog-Confirm”).dialog(“open”);
    });

    Thanks a lot, I really appreciate it!

    Ricardo

  • October 20, 2011 - 1:50 pm | Permalink

    Hey Jen,

    sorry to bother you… I am a novice java-script guy… and I love your modal tutorial.

    I implemented the jQuery UI Dialog into my site.. and I just want to get it working properly before styling it up for my site… however, I get the modal confirm popup.. but I don’t get the response back in the form when I confirm / submit.

    can you tell me why I am not getting the:

    jQuery UI worked!!! Your e-mail address is

    link to the form:
    http://…..

    thanks again Jen for the tutorial!

    Greg B

  • October 20, 2011 - 2:53 pm | Permalink

    @greg b

    It’s just an echo back from PHP. I added it to the post.

    I took out you email and link from your comment. Evil doers, ya know…

  • Caio Enokibara
    October 25, 2011 - 11:00 am | Permalink

    How can i pass more than 1 input in Thickbox Method?

  • October 25, 2011 - 11:12 am | Permalink

    @Caio Enokibara

    Yes.

  • Caio Enokibara
    October 25, 2011 - 11:19 am | Permalink

    Sorry, I should say. How do I get more than one input in thickbox?

    $(“p#TB-email”).html($(“input#emailTB”).val());
    tb_show(‘Verify Form Thickbox Style’,'TB_inline?height=155&width=300&inlineId=TBcontent’);

  • October 25, 2011 - 11:30 am | Permalink

    @Caio Enokibara

    $(“p#TB-email”).html($(“input#emailTB”).val() + ” ” + $(“input#your_other_input”).val());

  • Caio Enokibara
    October 25, 2011 - 11:34 am | Permalink

    I loved the website and thank you for your attention and help.
    Keep up the good work.

  • Caio Enokibara
    October 26, 2011 - 8:02 am | Permalink

    Sorry but if you can help me again, I would like to pass the “var” I created for an external php file receives a $ _POST [variable]…

    code:
    $(‘form#confirmaTB’).submit(function(e){
    var plano = $(“select#id_p”).val();
    var fid = $(“input#fid_p”).val();
    var contrato = $(“input#id_c”).val();

    //document.dadosTeste.getElementById(“dados”);
    //objetoDados.value = plano;

    e.preventDefault();
    .
    .
    .

  • October 26, 2011 - 8:37 am | Permalink

    @Caio Enokibara

    Your form action should be your external php file and the method should be post.

    $(‘form#confirmaTB’).submit(function(e){
    e.preventDefault();

    var plano = $(“select#id_p”).val();
    var fid = $(“input#fid_p”).val();
    var contrato = $(“input#id_c”).val();
    });

    Thickbox function:

    $(‘input#TBsubmit’).click(function(){
    document.testconfirmTB.submit();
    });

    In your exteranl PHP you can pick up the post vars as you would for any other form submit to PHP. Use $ _POST [input_name].

  • Caio Enokibara
    October 26, 2011 - 9:17 am | Permalink

    Now the thickbox does not open.

    In the file that is in the “include” I own $ _POST [id_plano];

    Code:

    $(‘form#confirmaTB’).submit(function(e){
    e.preventDefault();
    var plano = $(“select#id_plano”).val();
    var fid = $(“input#fid_pesquisado”).val();
    var contrato = $(“input#id_contrato”).val();
    });
    $(‘input#TBsubmit’).click(function(){
    document.confirmaTB.submit();
    });

    });

  • October 26, 2011 - 10:54 am | Permalink

    @Caio Enokibara

    That’s not the entire code you need. I only gave you the parts that you needed to replace.

  • Caio Enokibara
    October 26, 2011 - 11:19 am | Permalink

    I understood, but the rescue through the $ _POST is not working.

    I’ll keep trying and trying to somehow pass the value of “var” to a javascript var in php and so put in a field “hidden”.

    But thanks anyway, even if you can give me a light, Thanks in advance.

  • rootd
    December 13, 2011 - 5:35 am | Permalink

    Dear Jen,

    I using jquery-ui version for submit a query about 20 – 30 query on the same time to update the inventory. On waiting the process to be done(the confirm submit dialog still there). if i accidently doing one more click to the Submit Form button it will sent another process to server. Can Submit Form being disable after it being clicked? so it will be no more duplicated click

    tx

  • kropek4767
    December 16, 2011 - 10:30 am | Permalink

    Hi, sorry for my English :)

    I hava a problem with function code, I wan’t us this solutions for mor than one form with difrent id on the same site.

    "Zatwierd?": function() {
    document.protokol.submit();

  • 1 4 5 6

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>