Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
Ajax response from PHP script is skipping the IF statement in .done method

I am using the below code for a log in screen that uses an ajax call to check the user creds via the PHP script. As you can see I have put in a console call in which works but the if call is being skipped every time.

UPDATE *** I have realised that when I send the response to the console it seems to be an empty line then the echo from the PHP script on a new line.

Ajax Call

$(document).ready(function(){
// Variable to hold request
var request;

// Bind to the submit event of our form
$("#log").submit(function(event){

// Prevent default posting of form
event.preventDefault();

// Abort any pending request
if (request) {
    request.abort();
}
// setup some local variables
var $form = $(this);

// Let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");

// Serialize the data in the form
var serializedData = $form.serialize();

// Disable the inputs for the duration of the Ajax request.
$inputs.prop("disabled", true);

// Fire off the request
request = $.ajax({
    url: "../php/auth.php",
    type: "post",
    data: serializedData
});

request.done(function (response){

    console.log(response);

    if (response == "ok") {

        setTimeout(function() {
        return window.location.href = "../drive.php";
    }, 3000);
    }
    else {
        $('#error').html('<p class="text-center">'+response+'</p>');
    }
});

PHP Script

session_start();

include '***';

$email = clean($_POST['email']);
$pass = clean($_POST['pass']);

$_SESSION['name'] = $email;

$query = "SELECT * from users WHERE user='$email' and pass='$pass'";

$result = mysql_query($query);

$count = mysql_num_rows($result);

if ($count == 1) {
    echo 'ok';
}

else {

    echo 'Please Try Again';
};
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The php script was sending back whitespace and a new line hence why even though the console was outputting the correct return string it wasn't satisfying the if statement.

The include at the top of my PHP script had two extra lines of white space after the ?> closing tag. After removing this the response is now correct and satisfying the if statement on the Ajax call.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda