0
look at me here again, I cost to come here, but when I reach my limit, I am obliged, I need a help from you, I am trying to register with JQUERY and PHP, but it always made the registration successfully, even with all blank registration. Look at the codes:
PHP
if((isset($_POST['username'])) && (isset($_POST['password']))){
if( trim($_POST['username']) == '' ){
echo ('É obrigatório digitar seu usuário');
exit();
}
elseif( empty($_POST['password'] ) ) {
echo ('É obrigatório digitar sua senha');
exit();
}
if(!preg_match("/^([a-z-0-9 ]+)$/i",$_POST['username'])) {
echo "só letras e números!";
exit();
}
elseif( empty($_POST['password']) ) {
echo ('É obrigatório digitar sua senha');
exit();
}
elseif( empty($_POST['email'] ) ) {
echo ('É obrigatório digitar seu e-mail');
exit();
}
elseif( empty( $_POST['nome'] ) ) {
echo ('É obrigatório digitar seu nome');
exit();
}
elseif( empty( $_POST['sobrenome'] ) ) {
echo ('É obrigatório digitar seu sobrenome');
exit();
}
$username = trim(strtolower($_POST['username']));
$stmt = $mysqli->prepare("SELECT username FROM usuarios WHERE username=? LIMIT 1");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($id);
$stmt->store_result();
if($stmt->num_rows > 0) //To check if the row exists
{
echo "esse usuário já existe";
exit();
}
$username = strtolower($_POST['username']);
$username = str_replace(" ","",$username);
$query = "insert into usuarios(nome, sobrenome, username, email, nomecompleto, password) values ('". mysqli_real_escape_string($mysqli, $_POST['nome']) ."', '". mysqli_real_escape_string($mysqli, $_POST['sobrenome']) ."', '". trim($username) ."', '". mysqli_real_escape_string($mysqli, $_POST['email']) ."', '". mysqli_real_escape_string($mysqli, $_POST['nome']) ." ". mysqli_real_escape_string($mysqli, $_POST['sobrenome']) ."', '". md5(mysqli_real_escape_string($mysqli, $_POST['password'])) ."')";
$rs = $mysqli->query($query);
echo "1";
}
JQUERY
$(function($) {
$('#formcad').submit(function() {
// Limpando mensagem de erro
$("#resposta").html('');
// Enviando informações do formulário via AJAX
$(this).ajaxSubmit(function(resposta) {
// Se não retornado nenhum erro
if (!resposta)
// Redirecionando para o painel
window.location.href = 'cad.php';
$("#resposta").html(resposta);
if(resposta === 1){
}else{
$('#nome').val("");
$('#sobrenome').val("");
$('#password').val("");
$('#username').val("");
$('#email').val("");
$("#resposta").html("Você foi cadastrado com sucesso!");
}
});
// Retornando false para que o formulário não envie as informações da forma convencional
return false;
});
});
Do you know how to format the code on top? I think it will be clearer for everyone if it is formatted, for example: https://codepaste.net/9ycwwq
– Sergio
puts the console.log in each if to go debugging line by line
– Israel Zebulon
Two possibilities: 1) The problem is in ajaxSubmit(). Check what you’re getting in the "answer" value. 2) Generally AJAX calls take into account the HTTP return code you get back in the call (in the 200’s are successful status; 400 are client errors; among others). And that’s one more possibility of being the problem. In this case, besides giving "echo 'MENSAGEM_DE_ERRO';", you would need to define a PHP response header (using the header function), to specify the error.
– NewtonWagner
Your php returns the messages, missed that, missed that, and your jquery says "successfully registered" when the answer is different from 1, so this "all right" apparently...
– Jader A. Wagner
is no longer works
– Vinicius Henzel
Note the quotes in my "all right", you are saying that is to give registered successfully when the message comes other than 1, IE if it comes 'It is mandatory to type your user' will give registration successfully. pay more attention to the logics of Ifs to have fewer problems...
– Jader A. Wagner