ajaxcakephpform-submitdouble-submit-problem

Ajax form submit. form submitted twice


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.


Solution

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