如何使用JQuery和CakePHP提交表单?

时间:2015-10-09 14:42:55

标签: jquery cakephp cakephp-2.0

我有一个表单,我会使用JQuery将此表单提交给我的控制器而不使用CakePHP的JsHelper,但我不知道如何做到这一点。

我怎么能这样做?

我正在尝试这个。

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

0 个答案:

没有答案