0

I have a form which calls onSubmit="return report();". I have this javascript function in separate file. I would like to create my own jQuery UI Dialog instead of javascript confirm but I don't understand jQuery at all. How should I change javascript function report() to obtain jQuery UI Dialog?

There is part of my php file:

<form method="POST" action="add.php" name="alert" id="alert" onSubmit="return report();">
    //some inputs
    <button class='btn btn-primary' type='submit'>Send it</button>

And my js file:

 function report() {

    var report="Some text: \n\n";
    //There is code in simple javascript

    raport=raport+"\n";
    raport=raport+"If the are no mistakes click OK";
    return confirm(report);
    }

1 Answer 1

1

As I'm not a friend of inline-javascript and you want to use jQUery I modified your code a bit.

This code will bind an event-listiner on the submit-button and prevent the default action (the submit in this case). If the user confirm the dialog the submit-event will be triggered manually.

$('button[type=submit]').on('click', function (event) {

    event.preventDefault();

    var $this = $(this);
    var raport = "Some text: \n\n";
    //There is code in simple javascript
    raport = raport + "\n";
    raport = raport + "If the are no mistakes click OK";

    $("#dialog-confirm").text(raport).dialog({
        modal: true,
        buttons: {
            "OK": function () {
                $(this).dialog("close");
                $this.closest('form').submit();
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });

});

Demo

Reference

jQuery-UI dialog

dialog - buttons

.text()

.on()

attribute-selector

.closest()

Sign up to request clarification or add additional context in comments.

3 Comments

I don't know why but it doesn't work. Is there anything like onSubmit in jQuery?
It goes into $('button[type=submit]').on('click', function (event) , but dialog doesn't show. Is it because I have my js functions in separate file? I don't understand it. I included this file to my php code
@KasiaM any errors appearing in your console ? Have you included the jquery-ui-library ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.