Cakephp Submit form using Jquery?

Asked

Viewed 94 times

2

I have a form in Cakephp and I want to make the Submit of 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 native Jquery.

How to do this ?

I’m trying to do it like 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>    
  • This information that you put there is for the page do not refresh, if that’s what you want is in the right way, otherwise... And another, there are some mistakes made there

1 answer

0

If you’re going to help, you don’t 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.