I'm trying to INSERT INTO a database using ajax. The table is updated by the php file, but I always get an error in ajax and the page could not be redirected to the results page.
My 'POST' function, in the html page is (it returns always the error alert):
function enviaFormDetalhes() {
if (confirm("Tem a certeza que quer gravar os dados?")){
$.ajax( {
type: 'post',
url: 'php/f_propostas.php?tipo_acao=grava_nova_proposta&id_consulta='+getUrlVars()['id_consulta']+'&id_fornecedor='+getUrlVars()['id_fornecedor'],
data: $("#form_detalhes_proposta").serialize(),
success: function(data) {
alert("Dados gravados com sucesso");
location.href = 'f_editproposal.html?id_proposta='+data;
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
} );
} else {
return false;
}
}
The part of php file is: (This is working good, if I call i directly in the browser it returns me the lastinsertID):
case "grava_nova_proposta":
$ID_Fornecedor = $_GET['id_fornecedor'];
$ID_Consulta = $_GET['id_consulta'];
$DataRececao = $_POST['dt_proposta'];
$RefProposta = $_POST['ref_proposta'];
$DtValidade = $_POST['dt_validade'];
$DtCriacao_loop = gmdate('Y-m-d');
try {
$sql = "INSERT INTO fornecedores_propostas (
id_fornecedor,
id_contacto,
id_consultaloop,
datarecepcao,
ref_proposta,
validadeproposta)
VALUES (?, ?, ?, ?, ?, ?)";
$q = $conn->prepare($sql);
$q->execute(array($ID_Fornecedor, $ID_Contacto, $ID_ConsultaLoop, $DataRececao, $RefProposta, $DtValidade));
$Ultimo_ID = $conn->lastInsertId('id_proposta');
echo $Ultimo_ID;
break;
try
url: 'php/f_propostas.php,
data: tipo_acao=grava_nova_proposta&id_consulta='+getUrlVars()['id_consulta']+'&id_fornecedor='+getUrlVars()['id_fornecedor']+$("#form_detalhes_proposta").serialize(),