2011-01-28 111 views
0

我正在使用cakePHP,并且在我看来,我有一个具有ajax提交按钮的表单。我使用蛋糕助手呈现它。Ajax表单提交。表单提交了两次

<form method="post" class="form-class" id="form-id" name="form1" style="display: none"> 
       *[content for the form]* 
        <?php 
        echo $ajax->submit('Ok', 
          array(
             'id'=> 'submit1', 
             'url'=> array('controller'=>'c','action'=>'action1'), 
             'complete'=> 'jsfunction()' 

           )); 
        echo $form->button('Submit',array('id'=>'cancel','value'=>'Cancel','onClick'=>'clickCancel()')); 
        ?> 


</form> 

当我点击提交时,控制器动作被调用两次。如果存在此问题,我搜索了stackOverlow,但无法找到有效的解决方案。没有语法错误。

帮助将非常感激。谢谢。

+0

你有不同的浏览器吗? – RSK 2011-01-28 15:04:15

+0

你能从生成的页面显示任何代码吗? (我只能推测$ ajax-> submit()正在做什么,看到JS代码似乎是在客户端自动生成的)。 – 2011-01-30 05:17:22

回答

1

这听起来像AJAX事件处理程序不阻止默认行为。 尝试设置表单的onsubmit行为直列以防止它(如蛋糕的助手一样):

<form onsubmit="event.returnValue = false; return false;" method="post" class="form-class" id="form-id" name="form1" style="display: none"> 

我建议您查看FormHelper了一些解决这个有用的快捷键。

1

这是自动生成的代码:

$("#submit1367508668").bind('click', function(){ 
    $.ajax({ 
       async:true, 
       type:'post', 
       complete:function(request, json) { 
        getServerResponse(request) 
       }, 
       url:'/recipients/add', 
       dataType:'json', 
       data:$(this).parents('form:first').serialize()}); 
       blockUI(); 
    })