I'm doing a program and on the first page I enter a series of values entered by the user into the bank and generate a id
. After that I would like to return the value for the AJAX function and redirect to another page passing that id
as a parameter. Can anyone help me?
By inserting into the table (instead of returning 1
in variable cadastro
, I would like to return the inserted registry key):
$sql = 'INSERT INTO incidente (titulo, descricao, anonimo, tipo, foto)';
$sql .= 'VALUES (:titulo, :descricao, :anonimo, :tipo, :foto)' ;
try {
$recebeConexao = $pdo->prepare($sql);
$recebeConexao->bindParam(':titulo', $_POST['titulo'], PDO::PARAM_STR);
$recebeConexao->bindParam(':descricao', $_POST['descricao'], PDO::PARAM_STR);
$recebeConexao->bindParam(':anonimo', $_POST['anonimo'], PDO::PARAM_STR);
$recebeConexao->bindParam(':tipo', $_POST['tipo'], PDO::PARAM_STR);
$recebeConexao->bindParam(':foto', $_POST['img'], PDO::PARAM_STR);
$recebeConexao->execute();
if($recebeConexao == true){
$cadastro = 1;
}else{
$cadastro = 0;
}
} catch (PDOException $ex) {
echo "Erro inserção";
}
echo (json_encode($cadastro));
?>
AJAX function (I would like to call the other page by sending the value of id
):
function enviar(){
var formula = $('#formCliente').serialize();
var img = document.getElementById('smallImage').innerHTML;
$.ajax({
type:'POST',
data:formula,
url:'http://ip/connect/www/criar_incidente_camera.php',
success: function(data){
if(data == '' || data == 0){
alert('Usuário ou senha incorreto' + data);
window.location = "";
}
if(data == 1){
alert('Seja Bem-vindo!' + data);
window.location.href = "mapa_incidente.html";
} else {
alert('outro' + data);
}
}
Thank you for your attention!