Insert data in db with codeIgniter and ajax

0

Hello

I'm trying to insert data into my bank with ajax but I'm not getting it, it always returns me the error message

Controller:

    public function insert_user_data(){
    $this->load->model('crud');
    $this->crud->insert_user_data();
}

Model:

    public function insert_user(){
    $data_insert = array(
        'nome' => $this->input->post('nome'),
        'email' => $this->input->post('email'),
        'telefone' => $this->input->post('telefone'),
        'idade' => $this->input->post('idade')
    );
    $this->db->insert('usuarios', $data_insert);
}

view and jequery:

        <script type="text/javascript">
        $(document).ready(function(){
            $("#formInsert").submit(function(e){
                e.preventDefault(); 
                $.ajax({
                    url: "<?php echo site_url('/welcome/insert_user_data')?>",
                    type: "POST",
                    data: $("#formInsert").serialize(),
                    success: function(){
                        alert("foi");
                    },
                    error: function(){
                        alert("deu pau");
                    }

                });
            });
        });
    </script>



            <form id="formInsert" >
                Nome:
                <input type="text" name="nome" class="form-control"/>
                <br>
                E-mail:
                <input type="text" name="email" class="form-control"/>
                <br>
                Telefone:
                <input type="text" name="telefone" class="form-control" />
                <br>
                Idade:
                <input type="text" name="idade" class="form-control" />
                <br>
                <input  class="btn btn-default" type="submit" />
            </form>
    
asked by anonymous 28.03.2016 / 19:28

1 answer

-1

I got it, the problem was because I called the controller insert_user_data itself, not the model insert_user

The original controller code was:

public function insert_user_data(){
   $this->load->model('crud');
   $this->crud->insert_user_data();
}

Then I changed to:

public function insert_user_data(){
   $this->load->model('crud');
   $this->crud->insert_user(); //<--- linha modificada
}
    
28.03.2016 / 19:57