Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ;
    else
        return false ;
}

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

Thanks !

like image 795
Aditya P Bhatt Avatar asked Dec 01 '22 03:12

Aditya P Bhatt


2 Answers

A simpler method can also be used, Try this:

$this->Form->create('Request',array('onsubmit'=>'return confirm("are you sure?");'))
like image 120
Maadri Avatar answered Dec 04 '22 11:12

Maadri


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:

<?php
    $this->Html->scriptBlock("
        jQuery(function($){
            $('form.noncompetitor').submit(function(event){
                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(
        'type'=>'file','url'=>'/register',
        'default'=>false,'class'=>'noncompetitor'
    ));

    /* The rest of your form */
like image 33
Daniel Wright Avatar answered Dec 04 '22 10:12

Daniel Wright