Insert data into db with codeIgniter and ajax

Asked

Viewed 1,006 times

0

Hello

I am trying to insert data in my database with ajax but I am not getting, 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>
  • In the form tag, add method="post" see if it works, by default sending is by get

  • so your upload url is wrong, must be returning a 404, there is a Welcome controller with the function insert_user_data?

  • There is, the file Welcome.php already comes by default in codeIgniter, and there I posted the function insert_user_data of this controller, I don’t know if anything else is missing

  • Open the page and Apert ctrl+U see the generated html code see how the url is

  • http://[::1]/CI/index.php/Welcome/insert_user_data

  • I would do it here to solve the problem => failed to work with base_url in Codeigniter 2.2.6

  • keeps returning the error

  • url looks like this: /CI/index.php/Welcome/insert_user_data

  • you changed the site_url for base_url('/welcome/insert_user_data')?

  • yes, the difference is that it is without index.php /CI/Welcome/insert_user_data

  • 1

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

  • You can create an answer describing the details that solved the problem :)

Show 7 more comments

1 answer

-1

I got it, the problem was because I called on the controller insert_user_data himself, and not the model insert_user

The original controller code was:

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

So I switched to:

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

Browser other questions tagged

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