CakePHP submit form using JQuery?

2

I have a form in CakePHP and I want to submit this form using JQuery. I'm looking for a solution on how to do this without using CakePHP's JsHelper, I want to use only the same JQuery native.

How to do this?

I'm trying to do this.

//add.ctp

<script type="text/javascript">
    $(document).ready(function(){
        $('#formulario').submit(function(){
            var dados = $(this).serialize();

            $.ajax({
                type: "POST",
                url: "UsersController.php",
                data: dados,
                success: function( data )
                {
                    alert( data );
                }
            });

            return false;
        });
    });
    </script>


<div class="col-lg-6">
                <?php echo $this->Form->create('User', array("id"=>"formulario"); ?> 

                    <div class="form-group">                         
                        <?php echo $this->Form->input('nome', array( "label"=>"Nome",
                                                                     "placeholder"=>"Informe o nome",                                                                                               
                                                                     "class"=>"form-control",                                                                     
                                                                     ));?>                        

                    </div>

                    <div class="form-group">                                            
                        <?php echo $this->Form->input('email', array("label"=>"Email",
                                                                     "placeholder"=>"Informe o email",                                                                                               
                                                                     "class"=>"form-control",                                                                     
                                                                     ));?>                        

                    </div>

                    <div class="form-group">                                            
                        <?php echo $this->Form->input('senha', array("type"=>"password",                                                                    
                                                                     "maxlength"=>8,                                                                     
                                                                     "style"=>"width:200px;",
                                                                     "class"=>"form-control"));?>                        

                    </div>                

                    <button type="submit" class="btn btn-primary">Gravar</button>
                    <?php echo $this->Form->button("Limpar", array("type"=>"reset", "class"=>"btn btn-success"));?>
                <?php echo $this->Form->end(); ?>
            </div>    
    
asked by anonymous 09.10.2015 / 16:28

1 answer

0

If you are going to help, you do not have to do an Ajax

$(document).ready(function(){
        $("ID do Botão de Submit").click(function () { 
            document.querySelector("form").submit();
        })
    })

This function will perform the submit of your form in a simple way.

    
22.11.2017 / 13:47