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– rray
so your upload url is wrong, must be returning a 404, there is a Welcome controller with the function insert_user_data?
– rray
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
– zyzzete
Open the page and Apert
ctrl+U
see the generated html code see how the url is– rray
http://[::1]/CI/index.php/Welcome/insert_user_data
– zyzzete
I would do it here to solve the problem => failed to work with base_url in Codeigniter 2.2.6
– rray
keeps returning the error
– zyzzete
url looks like this: /CI/index.php/Welcome/insert_user_data
– zyzzete
you changed the
site_url
forbase_url('/welcome/insert_user_data')
?– rray
yes, the difference is that it is without index.php /CI/Welcome/insert_user_data
– zyzzete
I got it, the problem was because I called in the controller insert_user_data itself, and not the model insert_user
– zyzzete
You can create an answer describing the details that solved the problem :)
– rray