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 !
A simpler method can also be used, Try this:
You were actually 99% of the way there in the beginning .. you just needed to add 'return' before your call to the validation function.
Will do it.
There is the following step:-
- include this jquery file-
Create submit button
Jquery code
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:
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.
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:
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:
Hope this helps.