-1
so I’m trying to perform a request in Ajax to save a registration but it’s not working. I’m using Codeigniter. Every time I try to send the Form ajax is returning me the error I don’t know where the error might be
$('#enviarAltera').on('click', function(){
$.ajax({
url: base_url + '/index.php/contatos/salvarAlterar',
type: 'POST',
data: $('#form').serialize(),
dataType: 'json',
success: function(data){
if(data.status)
{
alert('O aluno foi inserido corretamente!');
}
},
error: function(){
alert('Ocorreu um erro ao tentar salvar o aluno.');
}
});
});
public function alterar($identificador)
{
$this->load->model("Contatos_Model");
$contato = $this->Contatos_Model->buscarId($identificador);
$pacote = array(
"contato" => $contato,
"pagina" => "contatoAlterar.php"
);
$this->load->view('index', $pacote);
}
//controller
public function salvarAlterar()
{
$id = $_POST['id'];
$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];
$this->load->model("Contatos_Model");
$this->Contatos_Model->salvarAlterar($id, $nome, $telefone, $email);
redirect("contatos");
}
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Atualiza Contatos</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<br>
<form name='form' id="form" class="form-horizontal form-label-left" method="POST">
<input type='hidden' name='id' value='<?php echo $contato[0]->id; ?>' />
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Nome: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type='text' id="nome" name='nome' value=<?php echo $contato[0]->nome; ?> id="first-name" class="form-control col-md-7 col-xs-12 ">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">E-mail: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type='text' id="email" name='email' value=<?php echo $contato[0]->email; ?> required="required" class="form-control col-md-7 col-xs-12 ">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Telefone:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input data-mask="(00) 00000-0000" id="telefone" class="form-control col-md-7 col-xs-12" type='text' name='telefone' value="<?php echo $contato[0]->telefone; ?>">
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<input id="enviarAltera" type="submit" class="btn btn-success" value="Alterar" name="enviarform">
</div>
</div>
</form>
</div>
</div>
</div>
If you read the documentation of callback error you will see that this callback accepts up to three parameters
Function( jqXHR jqXHR, String textStatus, String errorThrown )
provide these parameters print them and show us the actual error status.– Augusto Vasques