简体   繁体   English

CakePHP Javascript确认对话框表单提交取消不起作用

[英]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: 使用CakePHP的注册表单,一旦单击“提交”按钮,我只想显示“Javascript确认”对话框,该对话框的工作方式如下:

  • 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: CakePHP表格代码:

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

My JS Code: 我的JS代码:

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 ! 谢谢 !

也可以使用更简单的方法,试试这个:

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

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.) (而StackO的一个重要原因是不允许答案作者对接受编辑有最终决定权。)


Here's a solution using jQuery: 这是使用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 */

I think your function is failing because you are never actually returning the value from confirmfrmSubmit(). 我认为你的函数失败了,因为你实际上从来没有从confirmfrmSubmit()返回值。

Try adding the return part to the onSubmit field, like so: 尝试将返回部分添加到onSubmit字段,如下所示:

<?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. 您的JS代码应该按原样工作,问题只是在调用onSubmit处理程序时,确认值永远不会传递给表单。

PS note that you can do the following in the JS code: PS请注意,您可以在JS代码中执行以下操作:

return confirm("Text");

Hope this helps. 希望这可以帮助。

You were actually 99% of the way there in the beginning .. you just needed to add 'return' before your call to the validation function. 在开始时你实际上只有99%...你只需要在调用验证函数之前添加'return'。

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

Will do it. 会做的。

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(); 使用onsubmit函数,我只需定义'validatefrm(); return false;' 返回假;'

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

and found it working smooch :) 并发现它工作smooch :)

Let me know, if it helps you. 让我知道,如果它对你有帮助。

There is the following step:- 有以下步骤: -

- include this jquery file- - 包括这个jquery文件 -

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) {
            e.preventDefault();
                  var submitFormVar=1;
            if (submitFormVar!= 0) {
                $.confirm({
                    title: '',
                    content: 'Do you want to stop submit form' ,
                    buttons: {
                        ok: function () {
                            $(form).submit();
                        },
                        cancel: function (o) {
                            return o;
                        }
                    }
                });
            } else {
                $(form).submit();
            }
        });

Jquery code Jquery代码

  • if you want to submit the form then the value is 0 如果要提交表单,则值为0
  • if you want to not submit the form then the value is 1 如果您不想提交表单,则值为1
  • implement as your requirement logic 实现作为您的需求逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM