I have this small AJAX engine, I am using jQuery , this code is for a posting system, but it is not working.
The logic is for a form post register using 2 buttons to submit (submit) . One is to register only and the other is to register and publish the post. The status field will be saved with value 0
and when it will publish and register the attribute value for 1
.
//publicar e publicar e atualizar
var button = $('.cadPub');
var buttonCad = $('.cad');
var cadastro = $('.formDados');//formulario
cadastro.submit( function(){
if(button){
button.click(function(){
$('.status').each(function(){
var input = $(this);
input .val(1);
});
});
}else{
$('.status').each(function(){
var input = $(this);
input .val(0);
});
}
//CADASTRO
if(button || buttonCad) {
var id = $(this).attr('id');
var cap = $(this).attr('data-controller');
$.ajax({
url: BASE + cap,
data: {id: id},
type: "POST",
dataType: 'json',
beforeSend: function (data) {
$.each(alerts, function (key, value) {
$('.alert').removeClass(value);
});
},
success: function (data) {
if (data.retorno) {
$('.alerta').addClass(data.retorno[0]);
$('.icones').addClass(data.retorno[1]);//icone tem que passar no indice 1
$('.titulo').html(data.retorno[2]);//Titulo no indice 2
$('.result').html(data.retorno[3]);//Mensagem de retorno no indice 3
}
}
});
}
return false;
});