CakePHP Javascript Confirm dialog Form Submission cancel not working

Aditya P Bhatt picture Aditya P Bhatt · Mar 28, 2011 · Viewed 17.4k times · Source

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 !

Answer

Maadri picture Maadri · Jul 13, 2011

A simpler method can also be used, Try this:

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