0
My code has no return and I can’t figure out why.
index php.
<form id="login" method="POST">
<input type="text" name="username" placeholder="Usuário ou email" />
<input type="text" name="password" placeholder="Senha" />
<button onclick="enviar()" type="submit">Entrar</button>
</form>
<script type="text/javascript">
function enviar() {
var formula = $('#login').serialize();
$.ajax({
type: 'POST',
dateType: 'json',
data: formula,
url: 'http://localhost/teste/enviar.php',
success: function(data) {
if (data == 1) {
alert("OK");
window.location = "";
}
if(data == 0) {
alert("Nada OK");
window.location = "";
}
},
error: function(error) {
console.log(error.responseText);
}
})
return false;
}
</script>
<script type="text/javascript" src="js/jquery-2.1.3.js"></script>
Send.php
<?php
$resposta = 1;
echo (json_encode($resposta));
,what do you mean by "my function has no return?" the return would be to enter some "if", or return "false"? ,explain a little more of your problem
– Rodolfo Oliveira
He should be warning 'OK', since I’m returning 1
– Guilherme Pichok
I just tested it here and it worked perfectly, maybe the problem is in your "jQuery" file, you are sure it is in that location?
– Rodolfo Oliveira
Have you seen the browser console? Does the order that jQuery loading and the lack of the
$(document).ready
can’t be the problem?– Wallace Maxters
Try to remove the
json_encode
, let aloneecho $resposta;
. Only objects and arrays should be transformed to json.– Oeslei
Cancel the
submit
normal, and call its function in theonSubmit
thus:<form ..... onSubmit="enviar(); return false;" >
– rray