0

I have a form and I would submit this form to my controller using JQuery without use JsHelper of CakePHP but I don't know how I could do this.

How could I do this ?

I'm trying 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>    
3
  • I don't see any glaring issues with your code. Should work. What's the problem, what's not working, do you get any errors? Commented Oct 9, 2015 at 15:03
  • @Zealander the form doesn't submit Commented Oct 9, 2015 at 15:05
  • Any errors in the browser console? Hit F12 and go to Console tab. Commented Oct 9, 2015 at 15:07

2 Answers 2

2

This is an obvious error:

url: "UsersController.php",

The name of a file is not where the form should be posted you need to use a url such as the following:

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

            $.ajax({
                type: "POST",
                url:$(this).attr('action'),
                data: dados,
                success: function( data )
                {
                    alert( data );
                }
            });

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

Also your requested function should be allowed by auth as follows.

$this->Auth->allow(array('add', 'register','whatever_you_want'));
Sign up to request clarification or add additional context in comments.

Comments

0

You can also write your url like

url: "<?php echo Router::url(array('controller'=>'Users','action'=>'index'));?>",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.