I have the following scenario:
- I have a form that is used to register or change a teacher;
- To register I have a button at the top that calls the modal to do the POST;
- To change I have a button on each row of the teachers table that calls the same form, but populated with the data of the corresponding teacher to do the PUT;
- I have a JS file with AJAX requests, where I get the form from the id.
The problem is this: every time I click on the button of some line to change the teacher the form pops up properly but is submitted to the POST, this because of the form id. The submitting action for registration is always taking the form.
My AJAX are as follows:
$('#formSalvarProfessor').submit(function(event){
alert('POST');
event.preventDefault();
$.ajax({
url: "professores",
type: "POST",
data: $(this).serialize(),
dataType: "json",
success:function(data){
swal("Sucesso!", data.msg, "success");
}, error: function(data){
swal("Erro!", data.msg, "error");
}
});
});
$('.btn-info').click(function(){
$('#formSalvarProfessor').submit(function(event){
alert('PUT');
event.preventDefault();
var dados = $(this).data("dados");
var id = dados.ID_PROFESSOR_PRO;
$.ajax({
url: "professores/"+id,
type: "PUT",
data: $(this).serialize(),
dataType: "json",
success: function(data){
swal("Sucesso!", data.msg, "success");
}, error: function(data){
swal("Erro!", data.msg, "error");
}
});
});
});
How can I make each function capture the form properly?