Ajax form submit. form submitted twice

不想你离开。 提交于 2019-12-23 05:25:54

问题


I am working with cakePHP and in my view I have a form which has an ajax submit button. I rendered it using the cake helpers.

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

When I click submit, the controller action is called twice. I searched stackOverlow if this question existed but couldn't find a valid solution. There are no syntax errors.

Help will be really appreciated. Thanks.


回答1:


It sounds like the ajax event handlers aren't blocking the default behaviour. Try setting the form's onsubmit behaviour inline to prevent it (as cake's helper does):

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

I recommend checking out the FormHelper for some useful shortcuts around this.




回答2:


This is the auto generated code:

 $("#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(); 
     })


来源:https://stackoverflow.com/questions/4824859/ajax-form-submit-form-submitted-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!