2
I perform a query with ajax on a php page, the problem is that even asking to return data, the ajax 'date' does not return anything, I give an Alert in the 'date' and it returns me a blank alert, JS code below:
$(function(){
$(".logando").click(function(event){
event.preventDefault();
if($("#email").val() == "" || $("#senha").val() == "") {
$(".obrigatorio").slideDown(500).css("display","block");
}
else {
var emailUsuario = $("#email").val();
var senhaUsuario = $("#senha").val();
$.ajax({
type: "POST",
url: "../../controller/Logar_Cadastrar.inc.php",
data: {email: emailUsuario, senha: senhaUsuario},
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function() {
$(".obrigatorio").slideDown(100).html("Carregando");
},
success: function(data) {
alert(data);
}
})
}
})
})
Login block code HTML page:
<form action="../../controller/Logar_Cadastrar.inc.php" method="post">
<input type="text" name="email" required placeholder="Digite seu e-mail..." id="email"><br>
<input type="password" name="senha" required placeholder="Digite sua senha..." id="senha"><br>
<input type="hidden" name="logar">
<input type="submit" value="Logar-se" class="logando"><br>
</form>
Code of the PHP page:
<?php
require_once('../model/Logar_Cadastrar.class.php');
$logarCadastrar = new Logar;
//Função para logar
if(isset($_POST['logar'])):
$email = trim(strip_tags($_POST['email']));
$senha = trim(strip_tags($_POST['senha']));
$verificar = $logarCadastrar->Consulta("SELECT * FROM CS_usuarios WHERE email = ? AND senha = ?","ss","{$email}","{$senha}");
if($verificar >= 1):
// return "Encontrado";
echo "Encontrado";
else:
// return "Não encontrado";
echo "Não encontrado";
endif;
endif;
Ask your HTML code and PHP. It is impossible to answer without full code
– Amanda Lima
All right, I edited.
– Otavio Fagundes
Apparently the
if(isset($_POST['logar']))
is not being satisfied, so the code within theif
will not be executed. A possible solution is to take thisif(isset($_POST['logar']))
or switch to a variablelogar
in ajax:data: {email: emailUsuario, senha: senhaUsuario, logar:sim}
– Amanda Lima
Still no return, I’ve even changed the url but it still doesn’t work
– Otavio Fagundes
The
dataType: "json"
tells jQuery that your PHP will return JSON. But you are not returning JSON.– bfavaretto
can give me an example of how to proceed ?
– Otavio Fagundes
Tries with
dataType: "html"
and see if the php response comes.– bfavaretto
Following the @bfavaretto line, keep your Javascript code as is and in PHP you replace the "Echos" by:
echo json_encode(['retorno' => 'Encontrado'])
andecho json_encode(['retorno' => 'Não Encontrado'])
- The solution is for PHP 5.4 or sup. If your PHP is lower, you have to put array() instead of brackets.– Marco Garcia
But first of all you must pass a "log in" parameter in your ajax or remove that IF, as evaluated @Amandalima. The way it is does not pass the IF.
– Marco Garcia
Guys thank you very much, I got thanks to the help of all of you, I needed to use the if as Amanda told me, and I had to follow the tip of bfavaretto and otherwise I used json_encode as Marco recommended me, now everything is working perfectly, it was good that I got a good experience with it.
– Otavio Fagundes