1
The goal is that when the user clicks on a menu item, he loads the file containing a form into the "Other page". By the time this happens, the problem comes later. When I click on the form button that is loaded it redirects to the php page that saves the data in the database, so both the menu and the aside are gone. The goal is that it remains on the same page, or that it redirects itself, either only within the "Other Page" area. Looking on the Internet I thought I should use ajax, but I could not.
Ajax is like this
<script type="text/javascript">
$(document).ready(function(){
$('#btCadastrar').click(function(){
var nome= $('#txtnome').val();
var cpf = $('#txtcpf').val();
var senha = $('#txtsenha').val();
var nivel = $('#selectNivel').val();
$('form').submit(function(){
$.ajax({
type:"POST",
url:"cadusuarios.php",
data:{"nome":nome, "cpf":cpf,"senha":senha,"nivel":nivel},
error: function(){
alert("Erro ao Cadastrar");
}
});
});
});
});
</script>
<?php
include "conexao.php";
$nome = filter_input(INPUT_POST, 'nome');
$cpf = filter_input(INPUT_POST, 'cpf');
$senha = filter_input(INPUT_POST, 'senha');
$nivel = filter_input(INPUT_POST, 'nivel');
/*
$nome=utf8_encode($_POST['txtnome']);
$cpf=utf8_encode($_POST['txtcpf']);
$senha=utf8_encode($_POST['txtsenha']);
$nivel=($_POST['nivel']);
*/
session_start();
try
{
$sql= "Select nome,cpf,senha from c_usuarios where nome='$nome' and cpf='$cpf' and senha='$senha' ";
$rs = mysqli_query($conn,$sql);
if(mysqli_num_rows($rs)>0)
{
$_SESSION['cadastrado']='Usuário Já Cadastrado';
header('Location:Cadastrarusuarios.php');
}
else
{
$sql = "Insert into c_usuarios(nome,cpf,senha,nivel)values('$nome','$cpf','$senha','$nivel')";
$rs=mysqli_query($conn,$sql);
}
}
catch (Exception $e)
{
return $e->getMessage();
}
finally
{
mysqli_close($conn);
}
?>
you are using action in form? With ajax you do not need to use the action, otherwise it will redirect even
– lvr7
I was, but now I’ve changed and it’s done right. Thank you very much
– Diego_F