CakePHP Javascript Confirm dialog Form Submission cancel not working

With my CakePHP's registration form, once clicking Submit button, I simply want to display Javascript Confirm dialog box, which should work like:

  • If pressed Ok, should submit the form
  • If pressed Cancel, should not go for submit action

But here, when i press Cancel, though it gets submitted. Don't know why?

CakePHP Form Code:

<?php echo $form->create('Noncompetitor', array('type' => 'file', 'url' => '/register', 'onSubmit' => 'confirmfrmSubmit();'));?> 

My JS Code:

function confirmfrmSubmit(){

    var agree=confirm("Are you sure you wish to continue?");

    if (agree)
        return true ;
        return false ;

Please let me know, if you fellows have some idea on it.

Thanks !



Update: Credit to bicycle for fixing my broken callback. See below for details. (And a big raspberry to StackO for not allowing the answer author to have the final say on accepting an edit.)

Here's a solution using jQuery:

                if (!confirm('Are you sure?')) { return false; }
    ",array('inline'=>false)); // inline => false will put this snippet in the DOM's head.

    echo $this->Form->create('Noncompetitor',array(

    /* The rest of your form */

A simpler method can also be used, Try this:

$this->Form->create('Request',array('onsubmit'=>'return confirm("are you sure?");'))

I think your function is failing because you are never actually returning the value from confirmfrmSubmit().

Try adding the return part to the onSubmit field, like so:

<?php echo $form->create('Noncompetitor', array('type' => 'file', 'url' => '/register', 'onSubmit' => 'return confirmfrmSubmit();'));?>

Your JS code should work as it is, the problem is just that the confirm value is never being passed to the form when the onSubmit handler is called.

P.S. note that you can do the following in the JS code:

return confirm("Text");

Hope this helps.


I found a very simple solution to this query:

<?php echo $form->create('ModelName', array('type' => 'file', 'url' => 'url', 'name' => 'frmRegister', 'onsubmit' => 'validatefrm(); return false;')); ?>

With onsubmit function, I simply defined 'validatefrm(); return false;'

function validatefrm(){ // my condition, when i needs it to submitted document.frmRegister.submit(); return true; }

and found it working smooch :)

Let me know, if it helps you.


You were actually 99% of the way there in the beginning .. you just needed to add 'return' before your call to the validation function.

<?php echo $form->create('Noncompetitor', array(
       'type' => 'file', 
       'url' => '/register', 
       'onSubmit' => 'return confirmfrmSubmit();'));

Will do it.


There is the following step:-

- include this jquery file-

echo $this->Html->css('/css/jquery-confirm.min.css', ['block' => 'css']);

Create submit button

<?= $this->Form->button('<i class="fa fa-check"></i><span class="hidden-xs"> ' . __('Save') . '</span>', ['type' => 'submit', 'escape' => false, 'id' => 'submit_form_id', 'class' => 'btn green', 'title' => 'Save']); ?>

$('body').on('click', '#submit_form_id', function (e) {
                  var submitFormVar=1;
            if (submitFormVar!= 0) {
                    title: '',
                    content: 'Do you want to stop submit form' ,
                    buttons: {
                        ok: function () {
                        cancel: function (o) {
                            return o;
            } else {

Jquery code

  • if you want to submit the form then the value is 0
  • if you want to not submit the form then the value is 1
  • implement as your requirement logic


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.